I have product categories, that I have a built a navigation for. However, some of the categories have sub-categories, and some of the categories have sub-sub-categories, and so forth, perhaps as deep as sub-sub-sub-sub-sub-categories.
I want a nice tidy navigation that shows the structure like this:
main-category (parent)
-sub-category
-sub-category (parent)
--sub-sub-category
--sub-sub-category
--sub-sub-category(current category)
--sub-sub-category
-sub-category
-sub-category
main-category
So essentially you can view all other child categories, for the current category and parent categories. I have achieved this with a lot of code, but I am sure it could be done with a recursive function.
The information I have to build the navigation is an array of all the categories, starting with the main category, and going to child to child from there.
here is my current code:
$structure = array( [0] => 0 [1] => 72 [2] => 51); // example
$navigation = '<ul id="category_side_nav">';
$qry0 = mysqli_query($con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$structure[0]." AND `categories_status`=1 ORDER BY `sort_order`");
while($row0 = mysqli_fetch_assoc($qry0)) {
$navigation .= '<li class="navigation0"><a class="';
if((isset($structure[1]) && $row0['categories_id'] == $structure[1]) || $row0['categories_id']==$id) $navigation .= 'current_navigation';
else $navigation .= 'other_navigation';
$navigation .= '" href="'.$base.$row0['categories_url'].'/">'.ucwords($row0['categories_name']).'</a></li>';
if((isset($structure[1]) && $row0['categories_id'] == $structure[1]) || $row0['categories_id'] == $id) {
$cat1 = preg_replace("/_c[0-9_]+/", "", $row0['categories_url']).'/';
$qry1 = mysqli_query($con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$row0['categories_id']." AND `categories_status`=1 ORDER BY `sort_order`");
while($row1 = mysqli_fetch_assoc($qry1)) {
$navigation .= '<li class="navigation1"><a class="';
if((isset($structure[2]) && $row1['categories_id'] == $structure[2]) || $row1['categories_id']==$id) $navigation .= ' current_navigation';
else $navigation .= 'other_navigation';
$navigation .= '" href="'.$base.$cat1.$row1['categories_url'].'/">'.ucwords($row1['categories_name']).'</a></li>';
if((isset($structure[2]) && $row1['categories_id'] == $structure[2]) || $row1['categories_id'] == $id) {
$cat2 = $cat1.preg_replace("/_c[0-9_]+/", "", $row1['categories_url']).'/';
$qry2 = mysqli_query($con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$row1['categories_id']." AND `categories_status`=1 ORDER BY `sort_order`");
while($row2 = mysqli_fetch_assoc($qry2)) {
$navigation .= '<li class="navigation2"><a class="';
if((isset($structure[3]) && $row2['categories_id'] == $structure[3]) || $row2['categories_id']==$id) $navigation .= ' current_navigation';
else $navigation .= 'other_navigation';
$navigation .= '" href="'.$base.$cat2.$row2['categories_url'].'/">'.ucwords($row2['categories_name']).'</a></li>';
if((isset($structure[3]) && $row2['categories_id'] == $structure[3]) || $row2['categories_id'] == $id) {
$cat3 = $cat2.preg_replace("/_c[0-9_]+/", "", $row2['categories_url']).'/';
$qry3 = mysqli_query($con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$row2['categories_id']." AND `categories_status`=1 ORDER BY `sort_order`");
while($row3 = mysqli_fetch_assoc($qry3)) {
$navigation .= '<li class="navigation3"><a class="';
if((isset($structure[4]) && $row3['categories_id'] == $structure[4]) || $row3['categories_id']==$id) $navigation .= ' current_navigation';
else $navigation .= 'other_navigation';
$navigation .= '" href="'.$base.$cat3.$row3['categories_url'].'/">'.ucwords($row3['categories_name']).'</a></li>';
if((isset($structure[4]) && $row3['categories_id'] == $structure[4]) || $row3['categories_id'] == $id) {
$cat4 = $cat3.preg_replace("/_c[0-9_]+/", "", $row3['categories_url']).'/';
$qry4 = mysqli_query($con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$row3['categories_id']." AND `categories_status`=1 ORDER BY `sort_order`");
while($row4 = mysqli_fetch_assoc($qry4)) {
$navigation .= '<li class="navigation4"><a class="';
if((isset($structure[5]) && $row4['categories_id'] == $structure[5]) || $row4['categories_id']==$id) $navigation .= ' current_navigation';
else $navigation .= 'other_navigation';
$navigation .= '" href="'.$base.$cat4.$row4['categories_url'].'/">'.ucwords($row4['categories_name']).'</a></li>';
}
}
}
}
}
}
}
}
}
$navigation .= '</ul>';
The database only has category_id, and parent_id.
I couldnt create recursive function, because I needed to know the next entry in the array, so I could style the navigation accordingly, and that got me really stuck.
Can anyone help me turn this into a recursive function or at least some functions to make it tidier with less code.
// UPDATE
Ok I tried to think of a different way by incrementing the array like this, its a bit buggy though, but might be the way to proceed, if anyone has any ideas thinking more like this
function nav() {
$cat_level = structure($this->id);
$this->cats = array_reverse($cat_level);
$nav = '<ul id="category_side_nav">';
$nav .= $this->structure(0,BASE);
$navigation .= '</ul>';
return $navigation;
}
function structure($i,$url) {
if($i==0) $nav = '';
$qry = mysqli_query($this->con,"SELECT `categories_id`,`categories_name`,`categories_url` FROM `categories` WHERE `parent_id`=".$this->cats[$i]." AND `categories_status`=1 ORDER BY `sort_order`");
while($row = mysqli_fetch_assoc($qry)) {
$nav .= '<li class="navigation'.$i.'"><a class="';
if((isset($structure[$i+1]) && $row['categories_id'] == $this->cats[$i+1]) || $row['categories_id']==$this->id) $nav .= 'current_navigation';
else $nav .= 'other_navigation';
$nav .= '" href="'.$url.$row['categories_url'].'/">'.ucwords($row['categories_name']).'</a></li>';
if((isset($this->cats[$i+1]) && $row['categories_id'] == $this->cats[$i+1]) || $row['categories_id'] == $this->id) {
$url .= preg_replace("/_c[0-9_]+/", "", $row['categories_url']).'/';
$this->structure($i+1,$url);
}
}
return $nav;
}