click depth 0 and show all subs

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

Re: [rez] click depth 0 and show all subs

By Jason - October 1, 2011 - edited: October 5, 2011

Hi,

So when someone clicks on a category, you want to show all of the items in that branch (all subcategories of the selected category). Is that right?

What we can do then is if a category is selected, get all of the category nums in the branch and then select those items. Try this:

<?php
list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'menu_categories',

));

$branchCategoryNums = array(0);

if ($selectedCategory) {
// get all categories in the selected branch
list($branchCategoryRecords, ) = getCategories(array(
'tableName' => 'menu_categories',
'rootCategoryNum' => $selectedCategory['num'],
));

$branchCategoryNums = array_pluck($branchCategoryRecords, 'num');
}



list($itemRecords, $itemMetaData) = getRecords(array(
'tableName' => 'menu_items',
'allowSearch' => false,
'where' => "category IN (".join(",", $branchCategoryNums).")",
));

?>

<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if ($categoryRecord['depth'] == 0): ?>
<a href="<?php echo $categoryRecord['_link'] ?>" class="menuitem submenuheader"><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php endforeach; ?>


<?php if (!$selectedCategory): ?>
<p>Welcome to our menu page. Please select a menu category above.</p>
<?php endif ?>

<?php if ($selectedCategory): ?>
<h2 class="first-ct"><?php echo $selectedCategory['name'] ?></h2>
<?php if($selectedCategory['content']):?>
<?php echo $selectedCategory['content'] ?><br />
<?php endif?>

<ul class="menulist">
<?php foreach ($itemRecords as $items): ?>

<li>
<div class="item-head redtrans">
<div class="item-name"><?php echo $items['name'] ?></div>
<?php if($items['price']):?>
<span class="price"><?php echo $items['price'] ?></span>
<?php endif?>
</div>

<div class="clear"></div>

<?php if($items['description']):?>
<div class="item-dsc"><?php echo $items['description'] ?></div>
<?php endif?>
</li>

<?php endforeach ?>
</ul>

<?php endif ?>

<br />


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] click depth 0 and show all subs

By rez - October 2, 2011

thanks jason.

unfortunately, Im getting this error:

Warning: array_key_exists(): The second argument should be either an array or an object in /home/here/public_html/cmsAdmin/lib/common.php on line 1021 MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) ORDER BY dragSortOrder, ASC' at line 3

thanks for your help.

Re: [Jason] click depth 0 and show all subs

By rez - October 2, 2011

By the way, this happens after clicking on a category and the page is reloaded with the category parameters in the address bar. I checked and experimented with the "sorted by" in the editor and believe it's probably nothing to do with what's entered there? The same error happens even with just dragSortOrder.

Re: [rez] click depth 0 and show all subs

By Jason - October 3, 2011

Hi,

Ooops. This problem is due to a slight typo in the code I sent.

Try changing this line:

$branchCategoryNums = array_pluck($branchCategoryNums, 'num');

To this:

$branchCategoryNums = array_pluck($branchCategoryRecords, 'num');

I've made this change in the original post as well.

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] click depth 0 and show all subs

By rez - October 4, 2011 - edited: October 4, 2011

hmm. This code always displays the first category items no matter which category you select.

Re: [rez] click depth 0 and show all subs

By Jason - October 4, 2011

Hi,

I will probably need to take a quick look at the code in action. If you could fill out a [url http://www.interactivetools.com/support]2nd Level Support Request[/url] I can see what I can find.

Thanks
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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