click depth 0 and show all subs
7 posts by 2 authors in: Forums > CMS Builder
Last Post: October 4, 2011 (RSS)
By rez - September 30, 2011 - edited: September 30, 2011
It seems like you are talking about what I need in that thread but i just can't get it working.
Here is my set up:
A category menu editor and an items editor. Each item is assigned a category using num as value and breadcrumb as label. Each root category only contains a description. Each sub cat has all the products.
Viewer: I want to use the same page (I guess?) as the list and details. For the nav menu, i only want to list the root categories. When clicking a root link in the nav menu, the page reloads and should show the name of the root and each sub category with their products listed under them.
list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'menu_categories',
//'selectedCategoryNum' => '',
));
list($itemRecords, $itemMetaData) = getRecords(array(
'tableName' => 'menu_items',
// 'allowSearch' => false,
));
// for the root links:
<?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): ?>
<?php if ($items['category'] == $selectedCategory['num']): ?>
<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 endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<br />
that's just one version, I have tried the details page code, onelevel twolevel, etc. I can only get every thing in the entire category menu to show in a tree.
The problem with the above version is of course that when selecting a link (root) and reloading the page, I do not know how to display all the products in that branch. The category clicked is actually empty (depth zero only contains a description and doesnt have any items assigned to it) so my loop shows nothing. All the products are contained in sub categories and should only display for that root / branch.
hope that makes sense. How do you make the selected root show all sub categories and the items assigned to them in from items table. (I can only find working code that lists out all sub categores to begin with, then shows a list of products based on the sub category clicked)
Re: [rez] click depth 0 and show all subs
By Jason - October 1, 2011 - edited: October 5, 2011
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
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
Re: [rez] click depth 0 and show all subs
By Jason - October 3, 2011
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
Re: [rez] click depth 0 and show all subs
By Jason - October 4, 2011
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/