Conditional Display of Field

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

Hi there on a list viewer I want to change the heading based on the variable being passed using seo urls...

E.g. when I go to:
www.domain.com/listviewer.php/type-value-a/
www.domain.com/listviewer.php/type-value-b
www.domain.com/listviewer.php/type-value-c/

So I assume I need something like this

if type = Value A
<h1>Value A</h1>
elseif type = Value B
<h1>Value B</h1>
else type = Value C
<h1>Value C</h1>

Not sure on what code to use though. Any suggestions?

Many thanks.

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

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

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

brokerage.php 4K

Re: [ross] Conditional Display of Field

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