Complex If Statements

7 posts by 2 authors in: Forums > CMS Builder
Last Post: July 3, 2010   (RSS)

Re: [jtedescojam] Complex If Statements

By gkornbluth - July 3, 2010

Hi jtedescojam,

I’m assuming that there are a finite number of categories so this would be my approach. It’s not necessarily the only way to do it, but it’s pretty straight forward.

My first thought is that it might be easier to use a single value pull down list then a checkbox for the values, and make that a required field in your editor.

Then for the if statements, you could use some of the ideas from this recipe taken from my CMSB Cookbook. http://www.thecmsbcookbook.com

Hope it helps.

Best,

Jerry Kornbluth

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 ?>

If you wanted to test for any of a series of conditions.
Say a or b or c, you could use

<?php if ($your_table Record['your_field'] == "apples" || $your_tableRecord['your_field'] == "bananas" || $your_tableRecord['your_other_field'] == "pears"): ?>
Your code...
<?php else: ?>
Your code...
<?php endif ?>

Or

<?php if (record['your_field'] == "apples" || $record['your_field'] == "bananas" || $record['your_other_field'] == "pears"): ?>
Your code...
<?php else: ?>
Your code...
<?php endif ?>

If you want to test to see if a field contains a particular alphanumeric pattern,
Like seeing if apples is included in a multi item list, you can use the strpos function. Theoretically the strpos function returns the numeric position of the first occurrence of the pattern that you’re searching for, however, before it can do that it has to decide if the pattern exists in the string.

To test for the pattern “apples” in the string “bananas, apples, pears, raisins” you could use something like:

<?php if (strpos($your_table['your_field'], 'apples')): ?>
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.

Before leaving this topic, I was curious as to the rules for requiring an <?php endif; ?> to close an "if" statement.

Jason Sauchuk, a programmer with Interactive Tools cleared up the question:

"You only need <?php endif ?> if your "if" statement covers more than 1 <?php?> block.

Example:

<?php if($x==1?>

----some code---

<?php endif ?>



This could also be put into 1 <?php ?> block with no <?php endif ?>, as in:

<?php if($x==1){
---some code--
}
?>
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: Complex If Statements

By jtedescojam - July 3, 2010

Thanks for posting the generic 'if' statements instructions, It'll certainly help in the future... unfortunately this hasn't helped my issue. I have a specific page calling up all products with the room_type 'dining rooms' using the following header code:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "\\\\frigga/home/users/web/b2870/as.shopfurniture/admin/lib/viewer_functions.php";

list($collections_itemsRecords, $collections_itemsMetaData) = getRecords(array(
'tableName' => 'collections_items',
'allowSearch' => '0',
'where' => "room_type LIKE '%\tDining Room\t%'",
));
?>

Now, I want to display only products on this page that has the category "fireplaces" checked off... this is what's traditional, and I think this would work if it was a pull down list... but it has to be check marks...
<?php foreach ($collections_itemsRecords as $record): ?>
<?php if ($record['category'] == 'fireplaces'): ?>
<div id="product-listing">
<?php if ($record['images']): ?>
<a href="<?php echo $record['_link'] ?>"><?php foreach ($record['images'] as $upload): ?><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="<?php echo $record['collection'] ?>" class="thumbnail" /><?php break ?><?php endforeach ?></a>
<?php else: ?><a href="<?php echo $record['_link'] ?>"><img src="../images/img-notavailable-75x100.jpg" class="thumbnail" /></a><?php endif ?>
<h2><?php echo $record['collection'] ?></h2>
<p><?php echo $record['price'] ?></p>
</div>
<h2>
<?php endif ?>
<?php endforeach ?>

But I think because this is a checkbox sequence, it's going to be a bit more complicated. I have 1 record that has both dining rooms and fireplaces checked off.

thanks again for the assist.
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Complex If Statements

By gkornbluth - July 3, 2010

You might try strpos instead of just an if

In addition to the note in the recipe, here are 2 posts that might help.

http://www.interactivetools.com/iforum/gforum.cgi?post=76002

http://www.interactivetools.com/iforum/gforum.cgi?post=77647

Best,

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: Complex If Statements

By jtedescojam - July 3, 2010

Yeah that'll do it.. thank you! I'm going to have to buy the cookbook now! Thanks for the help.

Not sure you have the time.. not sure I have the time.. but if you're up for a challenge... I'd like to be able to do this without having to duplicate the following block of code for every single category...

<?php foreach ($collections_itemsRecords as $record): ?>
<?php if (strpos($record['category'], 'Chests')): ?>
<h3>Chests</h3><?php break ?><?php endif ?><?php endforeach ?>
<?php foreach ($collections_itemsRecords as $record): ?>
<?php if (strpos($record['category'], 'Chests')): ?>
<div id="product-listing">
<?php if ($record['images']): ?>
<a href="<?php echo $record['_link'] ?>"><?php foreach ($record['images'] as $upload): ?><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="<?php echo $record['collection'] ?>" class="thumbnail" /><?php break ?><?php endforeach ?></a>
<?php else: ?><a href="<?php echo $record['_link'] ?>"><img src="../images/img-notavailable-75x100.jpg" class="thumbnail" /></a><?php endif ?>
<h2><?php echo $record['item_name'] ?></h2>
<p><?php echo $record['price'] ?></p>
</div>
<?php endif ?>
<?php endforeach ?>
<div style="clear:both"></div>


Let me know if you know a way.
John Tedesco

Creative Director



JAM Graphics

Re: [jtedescojam] Complex If Statements

By gkornbluth - July 3, 2010

Hi jtedescojam,

I like clean code too, but these kinds of details usually go into my "Some day..." pile.

I'm sure that there are some more elegant approaches to this issue, but a big piece of me says, "If it ain't broke..."

So...

Glad it's working for you.

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: [gkornbluth] Complex If Statements

By jtedescojam - July 3, 2010

Thanks again
John Tedesco

Creative Director



JAM Graphics