Making a field only appear if it has data

4 posts by 3 authors in: Forums > CMS Builder
Last Post: June 9, 2008   (RSS)

By _kate_ - June 8, 2008

I am making an online portfolio. I want to have links to previews of pages. I made 3 textfields as each project would have at most 3 links to preview pages.

Some projects will have no links, some 1 others 3 etc. How can I get this information to only display when it has data in that field? I am trying to get a display something like the below >

View: <a href="<?php echo $record['store'] ?>" target="_blank">Store Front</a>, <a href="<?php echo $record['auction'] ?>" target="_blank">Auction Template</a>

Re: [sagentic] Making a field only appear if it has data

By _kate_ - June 8, 2008

Cool, thanks that works well. I'm using the below code >

<?php if ($record['store']): ?>View: <?php endif ?>

<?php if ($record['store']): ?><a href="<?php echo $record['store'] ?>" target="_blank">Store Front</a><?php endif ?>
<?php if ($record['auction']): ?><a href="<?php echo $record['auction'] ?>" target="_blank">Auction Template</a><?php endif ?>


Sometimes I may only have the Auction Template link, and with the above code in this form the word "View" wouldn't display if there was only an auction link. How can I edit the code so that it shows "View" if the record is "store" OR "auction"?

Also, if there is more than one link showing, is there anyway to get it to display a comma after each record (except the last one)?

Thanks :)

Re: [AgentBristow] Making a field only appear if it has data

By Dave - June 9, 2008

Hi AgentBristow,

You can use " or " like this to test on more than one condition:

<?php if ($record['store'] or $record['auction']): ?>
View:
<?php endif ?>

And if you only had two links, you could use the same if statement to print a comma in the middle if both were defined. Will that work for you?
Dave Edis - Senior Developer
interactivetools.com