Need help with IF statement
6 posts by 3 authors in: Forums > CMS Builder
Last Post: December 12, 2011 (RSS)
By sublmnl - February 22, 2011
Thanks for all the previous help!.
Lets say I have two linked images by each entry.
Maybe some entries only have one link/image but not both and other entries have both links/images. The images are the same for everyone (facebook, twitter icons) so the echo works well here. I may even have some entries that have no linked images.
I want to show the image only if the client puts a link for it in the CMS.
So something like this but need PHP to get it done.
---
If link in CMS, then show image
If no link in CMS then skip and move onto the next image and check for link.
If no links, then don't show images at all.
Makes sense?
Re: [sublmnl] Need help with IF statement
Here's an approach that might help.
I used it in my foreach loop to display a Facebook image as a link, only if the client entered a Facebook URL in the "facebook" field of the record.
The preg_match part of the code makes sure that an http:// is added to the URL if necessary:
<?php if ($record['facebook']): ?>
<?php if (!preg_match("/^http:\/\//", $record['facebook'])) {$record['facebook'] = "http://" . $record['facebook']; }
?>
<a class="alphabet" href="<?php echo $record['facebook'] ?>" target="_blank"> <img src="images/Facebook_icon.gif" alt="Facebook Logo" vspace="5" width="32" height="32" /></a>
<?php endif ?>
NOTE: You might want to implement the free removeExtraWhitespace plugin that Dave Edis created http://www.interactivetools.com/forum/gforum.cgi?post=84270;search_string=removeExtraWhitespace;#84270
to insure that an errant blank space doesn't trick the if statement into showing the image.
There's a lot more about working with if statements in my CMSB Cookbook www.thecmsbcookbook.com
Hope that get's you started.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Need help with IF statement
By sublmnl - February 24, 2011
Below is my code that I have with that works:
<?php foreach ($featuredRecords as $record): ?>
.....
....
....
...
<?php if ($record['event_venue_facebook_link']): ?><?php if (!preg_match("/^http:\/\//", $record['event_venue_facebook_link'])) {$record['event_venue_facebook_link'] = "http://" . $record['event_venue_facebook_link']; }
?><a href="<?php echo $record['event_venue_facebook_link'] ?>" target="_blank"><img src="/images/facebook_small22.png" /></a><?php endif ?><?php if ($record['event_venue_twitter_link']): ?><?php if (!preg_match("/^http:\/\//", $record['event_venue_twitter_link'])) {$record['event_venue_twitter_link'] = "http://" . $record['event_venue_twitter_link']; } ?><a href="<?php echo $record['event_venue_twitter_link'] ?>" target="_blank"><img src="/images/twitter_small22.png" /></a><?php endif ?>
<?php endforeach ?>
Re: [sublmnl] Need help with IF statement
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Need help with IF statement
By sublmnl - December 11, 2011 - edited: December 11, 2011
I love the 'If Link' thing we did earlier.... but what I want to do this now:
IF (checkbox) is Yes
Then show _link
I'm talking about the '_link' that is created from the list page to a detail page.
In my list record editor, I have a field that is a checkbox
<?php echo $record['has_a_page'] ?>
I only want to show the "_link" to the detail page if the checkbox is checked. I was going to wrap the <?php echo $record['title'] ?> with the IF ELSE statement because if the checkbox is not checked I still want to show the unlinked <?php echo $record['title'] ?> but I got stumped on this one.
Help?!
Re: [sublmnl] Need help with IF statement
By Jason - December 12, 2011
Try this:
<?php if ($record['has_a_page']):?>
<a href = "<?php echo $record['_link'];?>"><?php echo $record['title'];?></a>
<?php else:?>
<?php echo $record['title']; ?>
<?php endif ?>
Does this seem to be what you're looking for?
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/