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
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_pagewhich 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
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
Programmer
interactivetools.com
Re: [robin] If Statement Problem
By northernpenguin - June 6, 2011 - edited: June 6, 2011
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
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
Programmer
interactivetools.com
Re: [robin] If Statement Problem
By northernpenguin - June 6, 2011 - edited: June 6, 2011
<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php echo $categoryRecord['_isSelected'] . " " . $categoryRecord['open_in_new_page']; ?>
<?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 if ($categoryRecord['open_in_new_page'] == "1"): ?>
<b><a href="<?php echo $categoryRecord['url'] ?>" rel="self" target="_blank"><?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>
Ok, that works.... sort of. Because of the double if statements, I am getting double menu items.
How would I put both statements together?
Ragi
northernpenguin
Northern Penguin Technologies
"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke
Re: [northernpenguin] If Statement Problem
By robin - June 7, 2011
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>
Programmer
interactivetools.com
Re: [robin] If Statement Problem
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