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

By (Deleted User) - March 16, 2010

Thank you Gerry. YOur example allowed me to stp through my code piece by piece and figure out where I went wrong. Apparently i needed to add a colon.

I am not sure why my { structure didn't work since its my understanding that the colon is an alternative to the {} structure. Can I still use a else if I use a colon?

I will try and impliment for the "desert" and "main course" sections.

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