Hi there! I'm having some problems in this code... (plaincart)
I have 2 pages:
1. index.php (is my main page where is the left menu);
2. functions.php (where is the PHP functions, necessary to build
the menu at index.php).
----------
At this point all code is correct...
the main menu is showing like this...
1. Women
2. Men
3. Children
----------
When I click, for example at "Women", the menu is showing like this...
1. Women
- Coats
- Blazers
- Dresses
2. Men
3. Children
----------
But, my big question is: how to show the menu always like this:
1. Women
- Coats
- Blazers
- Dresses
2. Men
- Coats
- Blazers
- Shirts
3. Children
- Boys
- Girls
----------
**QUESTION:** How to show always the parent id of the main categories... Perhaps a little modification in functions.php > formatCategories(); ?
******************* index.php *******************
$catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
// get all categories
$categories = fetchCategories();
// format the categories for display
$categories = formatCategories($categories, $catId);
?>
<ul>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>">All Category</a></li>
<?php
foreach ($categories as $category) {
extract($category);
// now we have $cat_id, $cat_parent_id, $cat_name
$level = ($cat_parent_id == 0) ? 1 : 2;
$url = $_SERVER['PHP_SELF'] . "?c=$cat_id";
// for second level categories we print extra spaces to give
// indentation look
if ($level == 2) {
$cat_name = ' ยป ' . $cat_name;
}
// assign id="current" for the currently selected category
// this will highlight the category name
$listId = '';
if ($cat_id == $catId) {
$listId = ' id="current"';
}
?>
<li<?php echo $listId; ?>><a href="<?php echo $url; ?>"><?php echo $cat_name; ?></a></li>
<?php
}
?>
</ul>
******************* functions.php *******************
function formatCategories($categories, $parentId)
{
// $navCat stores all children categories
// of $parentId
$navCat = array();
// expand only the categories with the same parent id
// all other remain compact
$ids = array();
foreach ($categories as $category) {
if ($category['cat_parent_id'] == $parentId) {
$navCat[] = $category;
}
// save the ids for later use
$ids[$category['cat_id']] = $category;
}
$tempParentId = $parentId;
// keep looping until we found the
// category where the parent id is 0
while ($tempParentId != 0) {
$parent = array($ids[$tempParentId]);
$currentId = $parent[0]['cat_id'];
// get all categories on the same level as the parent
$tempParentId = $ids[$tempParentId]['cat_parent_id'];
foreach ($categories as $category) {
// found one category on the same level as parent
// put in $parent if it's not already in it
if ($category['cat_parent_id'] == $tempParentId && !in_array($category, $parent)) {
$parent[] = $category;
}
}
// sort the category alphabetically
array_multisort($parent);
// merge parent and child
$n = count($parent);
$navCat2 = array();
for ($i = 0; $i < $n; $i++) {
$navCat2[] = $parent[$i];
if ($parent[$i]['cat_id'] == $currentId) {
$navCat2 = array_merge($navCat2, $navCat);
}
}
$navCat = $navCat2;
}
return $navCat;
}