Understanding "IF" Statements - a Tutorial
6 posts by 3 authors in: Forums > CMS Builder
Last Post: October 26, 2010 (RSS)
By gkornbluth - November 29, 2009 - edited: November 29, 2009
I keep seeing questions about the correct use of the “if” statement to test for specific conditions.
I hope that this excerpt from the CMSB Cookbook clears up some of the confusion.
Best,
Jerry Kornbluth
http://www.thecmsbcookbook.com
UNDERSTANDING AND USING IF STATEMENTS
The if statement is one of the most powerful and valuable operators that you can use when designing your pages. You can test for many different scenarios and have your page output (or not output) specific code, depending on the outcome of your tests.
The problem is that many times, if you’ve made a mistake in your coding, you’ll get a blank page displayed in your browser and have no idea what your mistake was.
So here’s a short primer on the correct way to use this powerful tool.
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”.
So, if you’re using only one if statement to test for a particular condition. Say to test whether the field “your_field” is empty, then you’d use:
In a single record viewer:
<?php if ($your_tableRecord['your_field']): ?> ...your code...
<?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field']): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>
You can also put an if statement around a block of text to keep it from being run if there are no records available.
<?php if ($your_tableRecord['your_field']): ?>
<?php foreach ($your_tableRecord['your_field'] as $upload): ?>
... code to be run...
<?php endforeach ?>
<?php endif ?>
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 single record viewer:
<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>
If you wanted to test for the absence of particular contents in “your_field”.
Say, if the contents of “your_field” was anything other than “apples” then, you can use the not operator “!” :
In a single record viewer:
<?php if (!$your_tableRecord['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?>
<?php if (!$record['your_field'] == 'apples'): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>
To use a series of if statements to test for a series of condition, then you’d use:
in a detail viewer:
<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your field contains apples...
<?php elseif ($your_tableRecord['your_field'] == 'bananas'): ?> ...your field contains bananas...
<?php else: ?> ...your code...
<?php endif ?>
And in a list viewer:
<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field'] == 'apples'): ?> ...your field contains apples...
<?php elseif ($record['your_field'] == 'bananas'): ?> ...your field contains bananas...
<?php else: ?> ...your code...
<?php endif ?>
<?php endforeach; ?>
You’re not limited to a single or a single field condition either.
To test for multiple conditions, you can use:
(Note: The “!” works in this too)
<?php if ($your_tableRecord['field_a'] == 'apples' && $your_tableRecord['field_b'] == 'bananas' && $your_tableRecord['field_c'] == 'pears'): ?> ...Your record has all three...
<?php endif ?>
Or
<?php foreach ($testRecords as $record): ?>
<?php if ($record['field_a'] == 'apples' && $record['field_b'] == 'bananas' && $record['field_c'] == 'pears'): ?> ...Your record has all three...
<?php endif ?>
<?php endforeach; ?>
If you wanted to test for three conditions in a list viewer record.
Say, “field_a” empty, “field_b” empty and “field_c” not empty, you could use:
<?php if (!$record['field_a'] && !$record['field_b'] && $record['field_c']): ?> ...your code...
<?php endif ?>
This is by far not a comprehensive list of the possibilities for using if statements, but it should be enough to get you started.
USING IF STATEMENTS TO DEFINE OUTPUT BASED ON A FILE EXTENSION
If you need to publish specific code that’s based on the type of file that you’ve uploaded into an upload field, like a .swf flash file, here’s the trick. Just use:
<?php if ($upload['extension'] == 'swf'): ?>
or
<?php elseif ($upload['extension'] == 'swf'): ?>
You can use this for non upload fields as well:
<?php if ($your_fieldRecord['extension'] == 'swf'): ?>
or
<?php elseif ($your_fieldRecord['extension'] == 'swf'): ?>
Don’t forget that every (series of) if statement(s) must be closed with an <?php endif ?>
IF STATEMENTS THAT MEET MORE THAN ONE CONDITION
If you’ve got a table called “people and two fields “price” and “description” and you need a specific result depending on whether there’s information in one, the other or both fields, you can approach it this way. Remember that the operator “!” means not so !$record['price'] means that there’s no information in the price field.
<!-- If there’s no price and no description, just show the title. –>
<?PHP foreach ($peopleRecords as $record): ?>
<?PHP if (!$record['price'] && !$record['l_description']): ?><?PHP echo $record['title'] ?><?PHP endif ?>
<!-- If there’s no price, just a description , offer information only. –>
<?PHP if (!$record['print_price'] && $record['full_description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a><br>Click the title for more information.<?PHP endif ?>">
<!-- If there’s a price and no description, ask for the sale. –>
<?PHP if ($record['price'] && !$record['description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a>Click the title to buy.<?PHP endif ?>
<!-- If there’s a price and a description, offer information and ask for the sale. –>
<?PHP if ($record['price'] && $record[description']): ?><a href=”<?PHP echo $record['_link'] ?>”><?PHP echo $record['title'] ?></a>Click the title the for more information or to buy.<?PHP endif ?>
<?PHP endforeach ?>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Understanding "IF" Statements - a Tutorial
Could you advise another option of the if > then statement, as none of these seem to match what I'm requiring:
1. All of the country names are in Section Editor called country_list
2. All of the blurb for each country (and other info) is stored in the Section Editor called Country Intros
3. Country Intros pulls the country name from the country_list (in #1 above) and another field (of many) called country_intro_copy
4. Depending on the search result, if the visitor selects "Egypt", then they get search resuls for Egypt, and ideally the country name (country_list) and country intro copy (country_intro_copy) get added at the top of the search results page, and the specific tours below.
The question: how do I write the if (Egypt), then add the country name and country intro to the top of the search results page. As I have about 50 countries, I am trying to avoid having a hard coded results page for each. Is that possible? If you take a look at http://acac6262.staging-zeus.netregistry.net/tours_summary.php - the "BBB" at the top is the country name, and the country intro should be just below it.
Thanks in advance.
Re: [degreesnorth] Understanding "IF" Statements - a Tutorial
have a look at this post.
http://www.interactivetools.com/iforum/gforum.cgi?post=82283
There's also the concept of related records which was released in v2.03.
My CMSB Cookbook http://www.thecmsbcookbook.com has 2 recipes that might help as well.
USING RELATED FIELDS TO POPULATE PAGES FROM A MASTER “ADDRESS BOOK” OR “PRODUCT LIST”
and
USING THE MULTI-SELECT DROP DOWN LIST TO SELECT LISTINGS RELATED TO A MAIN CATEGORY
Hope some of this gets you going in the right direction
Best,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Understanding "IF" Statements - a Tutorial
Re: [degreesnorth] Understanding "IF" Statements - a Tutorial
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Understanding "IF" Statements - a Tutorial
By Jason - October 26, 2010
If you could fill out a Second Level Support Request here:
https://www.interactivetools.com/support/email_support_form.php
We can take a closer look at your code and could then advise you on the best way to do this.
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/