Conditional Display of Field

8 posts by 4 authors in: Forums > CMS Builder
Last Post: July 27, 2009   (RSS)

Re: [aquaman] Conditional Display of Field

By gkornbluth - July 19, 2009

If statement are really versatile and powerful concepts.

If you think that an "If" statement will do the job for you, then here's some code that should work for you.

The first rule to remember is that every (series of) if statement(s) must be closed with an <?php endif ?> in order for them to work correctly.

The second rule is that if you’re using a series of if statements, then the first one is always an “if”, the last is always “else” and any in the middle are “elseif”.

If you wanted to test for particular contents in “your_field”. Say, if the contents of “your_field” equaled “apples” then, you’d use the exactly equal to “==” operator:

in a detail viewer (single record):


<?php if ($your_tableRecord['your_field'] == 'apples'): ?><h1>your field contains apples</h1>
<?php elseif ($your_tableRecord['your_field'] == 'bananas'): ?> <h1>your field contains bananas</h1>
<?php else: ?><h1>your field contains no apples or bananas</h1>
<?php endif ?>


And in a list viewer(multi record) :

<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field'] == 'apples'): ?><h1>your field contains apples</h1>
<?php elseif ($record['your_field'] == 'bananas'): ?> <h1>your field contains bananas</h1>
<?php else: ?><h1>your field contains no apples or bananas</h1>
<?php endif ?>
<?php endforeach; ?>


Best,

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] Conditional Display of Field

By theclicklab - July 19, 2009

Hi Jerry,

Thanks for that it worked with the addition of <?php break ?>

But I think I'm trying to do this the wrong way because it grabs the first record when showing "ALL" types and if there are no records then it dos not show anything.

example - see red text: http://www.staging.baobabmarine.com/brokerage.php

changing the drop-down displays the three different types.

Is there a better way of doing this?

Re: [aquaman] Conditional Display of Field

By ross - July 21, 2009

Hi there.

Thanks for posting!

Could you post a copy of that whole page you are working with? Best case scenario would be if you could attach the file instead of pasting code into a post.

I'll take a look at how you have things setup and see what options you have. I definitely think you are on the right track and Jerry's tips on if blocks was great.

Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Conditional Display of Field

By theclicklab - July 21, 2009

Hi Ross, that's great, much appreciated. File attached.
Attachments:

brokerage.php 4K

Re: [aquaman] Conditional Display of Field

By ross - July 24, 2009

Hi there.

I've been looking through that code and usually, if you just go to brokerage.php without any search criteria, it will display all your records. The only thing right now is that the link to this page on your site doesn't actually work anymore. Could you send me an up to date link?

Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Conditional Display of Field

By theclicklab - July 24, 2009

Hi Ross, the site has gone live and is now at http://www.baobabmarine.com/brokerage.php - I have commented out the code so it does not show but its still viewable in the source.

Re: [aquaman] Conditional Display of Field

By Dave - July 27, 2009

Hi aquaman,

Are you still working on displaying a different heading based on the seo url variable?

If so, you can test urls like this:
www.domain.com/listviewer.php/type-value-a/
www.domain.com/listviewer.php/type-value-b

With this code:
<?php if (@$_REQUEST['type'] == 'value-a'): ?>
Value A
<?php endif ?>

<?php if (@$_REQUEST['type'] == 'value-b'): ?>
Value B
<?php endif ?>

Let me know if that works for you.
Dave Edis - Senior Developer

interactivetools.com