strange category menu / list alignment

5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 14, 2011   (RSS)

By rez - September 14, 2011

Hi, I'm guessing there may be a mistake in the code that could cause various weirdness (explained below if needed)? i just want everything in this nav menu to stay put like on first arrival except making the selected category bold on each query. Each link reloads the the same page and adds the results in a content area (no separate details page). But the problem is that clicking some links, shifts the indentations and adds bullets to main categories. i'm not sure why.

list($menuCategoriesRecords, $menuCategoriesMetaData) = getCategories(array(
'tableName' => 'menu_categories',
'rootCategoryNum' => 23,
));


<p class="spost"><ul class="menunav">
<?php foreach ($menuCategoriesRecords as $menuCategoryRecord): ?>
<?php echo $menuCategoryRecord['_listItemStart'] ?>
<?php if ($menuCategoryRecord['_isSelected']): ?>
<strong><?php if (!$menuCategoryRecord['_hasChild']):?><a href="<?php echo $menuCategoryRecord['_link'] ?>"><?php endif; ?><?php echo $menuCategoryRecord['name'] ?><?php if (!$menuCategoryRecord['_hasChild']):?></a><?php endif; ?></strong>
<?php else: ?>
<?php if (!$menuCategoryRecord['_hasChild']):?><a href="<?php echo $menuCategoryRecord['_link'] ?>"><?php endif; ?><?php echo $menuCategoryRecord['name'] ?><?php if (!$menuCategoryRecord['_hasChild']):?></a><?php endif; ?>
<?php echo $menuCategoryRecord['_listItemEnd'] ?><?php endif; ?>
<?php endforeach; ?>
</ul></p>


On page arrival, the above navigation lists as expected.
Each category lists without a bullet.
each child element has a bullet and is a link.
child items / links are indented under the category. (this is where the problem comes later):
Sandwiches
-Classic Sandwiches
-Panini Sandwiches
-Breakfast Sandwiches
Soups and Salads
-Soups
-Salads
Beverages
-Cafe
-Various
Delicatessen
-Cheese
-Meats
Catering
-Party Trays
-Catering Dishes
-Desserts


Clicking child 1 or 2 of sandwiches reloads the page and the nav looks the same but selection is bold. Perfect.

Clicking child 3 of sandwiches or any child >1 in other categories, reloads the the page and every thing below what was clicked indents and a bullet is added to each main category. Below, I clicked on Beverages:various (beverages became a child of soups and salads on reload and everything below it was indented and bulleted)

Sandwiches
-Classic Sandwiches
-Panini Sandwiches
-Breakfast Sandwiches
Soups and Salads
-Soups
-Salads
-Beverages
-Cafe
-Various
-Delicatessen
-Cheese
-Meats
-Catering
-Party Trays
-Catering Dishes
-Desserts


Yeah, i know. uploading and posting a link would be so much easier. :b Hopefully, that was clear and an easy fix. thanks for your support.

Re: [rez] strange category menu / list alignment

By Jason - September 14, 2011

Hi rez,

I think the issue here is the placement of your last "endif".

In your foreach loop, you output _listItemStart before your if statement. However, you output _listItemEnd inside the else section of your if statement.

Try changing it to this instead:

<?php foreach ($menuCategoriesRecords as $menuCategoryRecord): ?>
<?php echo $menuCategoryRecord['_listItemStart'] ?>
<?php if ($menuCategoryRecord['_isSelected']): ?>
<strong><?php if (!$menuCategoryRecord['_hasChild']):?><a href="<?php echo $menuCategoryRecord['_link'] ?>"><?php endif; ?><?php echo $menuCategoryRecord['name'] ?><?php if (!$menuCategoryRecord['_hasChild']):?></a><?php endif; ?></strong>
<?php else: ?>
<?php if (!$menuCategoryRecord['_hasChild']):?><a href="<?php echo $menuCategoryRecord['_link'] ?>"><?php endif; ?><?php echo $menuCategoryRecord['name'] ?><?php if (!$menuCategoryRecord['_hasChild']):?></a><?php endif; ?>
<?php endif ?>
<?php echo $menuCategoryRecord['_listItemEnd'] ?>

<?php endforeach; ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] strange category menu / list alignment

By rez - September 14, 2011 - edited: September 14, 2011

sigh.. I need to improve my troubleshooting approach, i should see this type of thing by now. Yes, problem resolved. Thank you!

Re: [rez] strange category menu / list alignment

By Jason - September 14, 2011

Hi rez,

No problem. I don't know how many times I've made those types of mistakes myself.

One technique for reducing those types of problems (or at least making them easier to spot) is to put any loop or conditional structures on their own line.

For example, instead of

<?php if ($x == 2): ?> Do something here <?php else: ?> do something else <?php endif ?>

try
<?php if ($x == 2): ?>
Do Something
<?php else: ?>
Do Something Else
<?php endif ?>


This example is simple enough, but as soon as you have if statements nested inside other if statements that are inside a loop, it's easy to loose track of where you are. Indenting nested statements is also a good way of keeping your code clear.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/