if else and CMS

5 posts by 3 authors in: Forums > CMS Builder
Last Post: March 17, 2010   (RSS)

By (Deleted User) - March 16, 2010 - edited: March 16, 2010

On http://www.gourmetguy.biz/menu_examples.php I am trying to create a list page that only lists items which are "featured" and under the appropriate category which is defined by a list field in the editor. Items can be an "Appetizer", "Main Course", or "Desert."

At present the code I have is

<div class="entry"><h3>Appetizers</h3><br/>
<?php foreach ($menu_examplesRecords as $menu_examplerecord): ?>
<?php
if ($menu_examplerecord['course'] = "Appetizer") {
<strong>echo $menu_examplerecord['title'];</strong>
echo $menu_examplerecord['description_menu'];<br/>
}
else {echo "No seasonal appetizers are featured at this time!";}
?>
<?php endforeach ?>

This is showing a blank screen (a most annoying feature of PHP since you can't even tell where it is going wrong.)

I use this to exclude those which are not featured:

list($menu_examplesRecords, $menu_examplesMetaData) = getRecords(array(
'tableName' => 'menu_examples',
'allowSearch' => '0',
'where' => 'featured = 1',
));

My plan is to have a similar section for each type of item after getting "appetizers" to work.

Where exactly am I going wrong?

Re: [cohagan] if else and CMS

Hi Cohagan,

Here's an excerpt from my CMSB Cookbook http://www.thecmsbcookbook.com that might help.

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

Hope that helps you to clear up the issues you're having.

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: [cohagan] if else and CMS

By gkornbluth - March 16, 2010 - edited: March 16, 2010

Here's some more from the CMSB Cookbook http://www.thecmsbcookbook.com that shows the use of elseif and else:

If you’re using it on multi record a list page:

<?PHP foreach ($pub_category_listRecords as $record): ?>
<?PHP if ($record['group_code'] == "book"): ?>
<?PHP echo $record['fieldA'] ?>
<?PHP elseif ($record['group_code'] == "CD"): ?>
<?PHP echo $record['fieldB'] ?>
<?PHP elseif ($record['group_code'] == "Magazine"): ?>
<?PHP echo $record['fieldC'] ?>
<?PHP else: ?>
<?PHP echo $record['fieldD'] ?>
Sorry, couldn't find a match for '<?PHP echo $record['group_code'] ?>'
<?PHP endif ?>
<?PHP endforeach; ?>

And if it’s on a single page or a detail page:

<?PHP if ($pub_category_listRecord['group_code'] == "book"): ?>
<?PHP echo $pub_category_listRecord['fieldA'] ?>
<?PHP elseif ($pub_category_listRecord['group_code'] == "CD"): ?>
<?PHP echo $pub_category_listRecord['fieldB'] ?>
<?PHP elseif ($pub_category_listRecord['group_code'] == "Magazine"): ?>
<?PHP echo $pub_category_listRecord['fieldC'] ?>
<?PHP else: ?>
<?PHP echo $pub_category_listRecord['fieldD'] ?>
Sorry, couldn't find a match for '<?PHP echo $pub_category_listRecord['group_code'] ?>'
<?PHP endif ?>

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] if else and CMS

By Chris - March 17, 2010 - edited: March 17, 2010

Hi cohagan,

I'd strongly recommend getting your PHP to report errors. I forget semicolons all the time and without simple and fast error reports I would probably have torn all my hair out by now. If you google for [php blank page], you should find some solutions documented.

I think the error is that you have HTML in your PHP tags:

<div class="entry"><h3>Appetizers</h3><br/>
<?php foreach ($menu_examplesRecords as $menu_examplerecord): ?>
<?php
if ($menu_examplerecord['course'] = "Appetizer") {
<strong>echo $menu_examplerecord['title'];</strong>
echo $menu_examplerecord['description_menu'];<br/>
}
else {echo "No seasonal appetizers are featured at this time!";}
?>
<?php endforeach ?>


One way to fix that is to wrap your HTML in echos:

echo "<strong>";
echo $menu_examplerecord['title'];
echo "</strong>";
echo $menu_examplerecord['description_menu'];
echo "<br/>";


There's another problem with your code: the else. Let's say your getRecords() returns 2 featured records, but they're "Main Course" and "Desert". Each time the IF runs, it'll run the ELSE code and you'll end up with your "No seasonal appetizers..." message repeated.

A simple solution would be to move your "course=" test into your WHERE:

list($featuredAppetizerRecords, ) = getRecords(array(
'tableName' => 'menu_examples',
'allowSearch' => '0',
'where' => "featured = 1 AND course='Appetizer'",
));


Then you can use the more conventional:

<?php foreach ($featuredAppetizerRecords as $featuredAppetizer): ?>
<strong><?php echo $menu_examplerecord['title'] ?></strong>
<?php echo $menu_examplerecord['description_menu'] ?><br/>
<?php endforeach ?>
<?php if (!$featuredAppetizerRecords): ?>
No seasonal appetizers are featured at this time!<br/>
<?php endif ?>


With this approach, you can pretty much copy-and-paste for the other two courses.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris