Need help with IF statement

6 posts by 3 authors in: Forums > CMS Builder
Last Post: December 12, 2011   (RSS)

Reaching out to the board once again.
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

Makes great sense.

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
The first CMS Builder reference book is now available on-line!







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

Works Jerry. Thanks and I have your online book. I should refer to it more often.

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

Glad you got it going.

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [sublmnl] Need help with IF statement

By Jason - December 12, 2011

Hi,

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/