If Statement Problem

7 posts by 2 authors in: Forums > CMS Builder
Last Post: June 7, 2011   (RSS)

By northernpenguin - June 5, 2011 - edited: June 5, 2011

Hi Everyone

I am using the following code to display my menu (which works great), and have added additional functionality which is not working correctly.

Original code:
<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<b><a href="<?php echo $categoryRecord['url'] ?>" rel="self"><?php echo $categoryRecord['name'] ?></a></b>
<?php else: ?>
<a href="<?php echo $categoryRecord['url'] ?>" rel="self"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>


I added a new checkbox field labelled
open_in_new_page
which basically identifies the menu entry as a URL that needs to be opened in a new tab/page. Here is the new code:

<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected'] && $categoryRecord['open_in_new_page'] == "0"): ?>
<b><a href="<?php echo $categoryRecord['url'] ?>" rel="self"><?php echo $categoryRecord['name'] ?></a></b>
<?php else: ?>
<a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>


Unfortunately, all selected menu items open in a new tab/window!

Any help to solve this issue would be appreciated!

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [northernpenguin] If Statement Problem

By robin - June 6, 2011

Hey Ragi,

Something is causing the if statement to return false. Time for some debugging.

You can try a debugging trick and echo the values to see what's going on. Like temporarily adding a line just above the if:

<?php echo $categoryRecord['_isSelected'] . " " . $categoryRecord['open_in_new_page']; ?>

Also it never hurts to be explicit on order when you're using conditions. Something like this:
<?php if ($categoryRecord['_isSelected'] && ($categoryRecord['open_in_new_page'] == "0")): ?>

Hope that helps,
Robin
Robin
Programmer
interactivetools.com

Re: [robin] If Statement Problem

By northernpenguin - June 6, 2011 - edited: June 6, 2011

Robin: Very weird. It looks like the second condition is being ignored. You can see it for yourself at http://bc.aircadetleagueofcanada.ca.

I left the debugging code in.

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [northernpenguin] If Statement Problem

By robin - June 6, 2011

Hey,

hmmmm right now the if condition always evaluates to false because is_selected is 0.

Are you checking is_selected to determine if the link should be bold and checking open_in_new_page to check if the link should open in a new window? If that is the case they should be in separate if statements since they are for different functionality.

Hope that helps,
Robin
Robin
Programmer
interactivetools.com

Re: [northernpenguin] If Statement Problem

By robin - June 7, 2011

Hey Raji,

Here is a little re-arranging of the code to get you going.

Hope that helps,
Robin


<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php echo $categoryRecord['_isSelected'] . " " . $categoryRecord['open_in_new_page']; ?>

<?php if ($categoryRecord['_isSelected']): ?>
<b>
<?php endif; ?>

<?php if ($categoryRecord['open_in_new_page'] == "1"): ?>
<b><a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>

<?php if ($categoryRecord['_isSelected']): ?>
</b>
<?php endif; ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>

Robin
Programmer
interactivetools.com

Re: [robin] If Statement Problem

Thanx Robin!

I just made one minor change. I removed the code in red, so that only external sites open a new page.

<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php echo $categoryRecord['_isSelected'] . " " . $categoryRecord['open_in_new_page']; ?>

<?php if ($categoryRecord['_isSelected']): ?>
<b>
<?php endif; ?>

<?php if ($categoryRecord['open_in_new_page'] == "1"): ?>
<b><a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>

<?php if ($categoryRecord['_isSelected']): ?>
</b>
<?php endif; ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>


Works great!

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke