Hi, I'm trying to create a theme-compatible category system, but I missed some points.
For example;
category_name_women_clothing
and category_name_women_bag
, etc.. are supposed to be collapsible, but I was not successful in this.
How can I make a model that opens and closes sub-categories, where am I missing?
This is what it looks like right now - Image
PHP Code:
function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1)
{
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent_id']) {
if ($currLevel > $prevLevel) echo "<ul id='" . $category['parent_id'] . "' class='collapse show' data-bs-parent='#" . $category['parent_id'] . "'>";
if ($currLevel < $prevLevel) {
}
if ($currLevel == $prevLevel) {
}
echo '<li><a class="widget-link" href="#' . $category['parent_id'] . '" ' . (($currLevel == 0) ? 'data-bs-toggle="collapse"' : '') . '>' . $category['name'] . '</a>';
if ($currLevel > $prevLevel) {
$prevLevel = $currLevel;
}
$currLevel++;
createTree($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo "</li></ul>";
}
Sample HTML:
<div class="widget widget-categories">
<h3 class="widget-title">Collapsible categories</h3>
<ul id="categories">
<li>
<a class="widget-link" href="#clothing" data-bs-toggle="collapse">
Clothing
<small class="text-muted ps-1 ms-2">235</small>
</a>
<ul class="collapse show" id="clothing" data-bs-parent="#categories">
<li><a class="widget-link" href="#">Blazers & Suits</a></li>
<li><a class="widget-link" href="#">Cardigans</a></li>
<li><a class="widget-link" href="#">Dresses</a></li>
</ul>
</li>
<li>
<a class="widget-link collapsed" href="#shoes" data-bs-toggle="collapse">
Shoes
<small class="text-muted ps-1 ms-2">210</small>
</a>
<ul class="collapse" id="shoes" data-bs-parent="#categories">
<li><a class="widget-link" href="#">Athletic shoes</a></li>
<li><a class="widget-link" href="#">Balerinas & Flats</a></li>
<li><a class="widget-link" href="#">Boots</a></li>
</ul>
</li>
<li>
<a class="widget-link collapsed" href="#electronics" data-bs-toggle="collapse">
Electronics
<small class="text-muted ps-1 ms-2">197</small>
</a>
<ul class="collapse" id="electronics" data-bs-parent="#categories">
<li><a class="widget-link" href="#">Computers & Accessories</a></li>
<li><a class="widget-link" href="#">TV, Video & Audio</a></li>
<li><a class="widget-link" href="#">Smartphones & Tablets</a></li>
</ul>
</li>
<li>
<a class="widget-link collapsed" href="#accessories" data-bs-toggle="collapse">
Accessories
<small class="text-muted ps-1 ms-2">124</small>
</a>
<ul class="collapse" id="accessories" data-bs-parent="#categories">
<li><a class="widget-link" href="#">Bags</a></li>
<li><a class="widget-link" href="#">Belts</a></li>
<li><a class="widget-link" href="#">Hats</a></li>
</ul>
</li>
</ul>
</div>
Database:
| cID | cPID (parent ID) | cSlug | cName |
|-----|-------------------|-------|-----------------------------------------|
| 1 | 0 | | category_name_women |
| 2 | 0 | | category_name_men |
| 3 | 0 | | category_name_baby |
| 4 | 0 | | category_name_women_clothing|
| 5 | 1 | | category_name_women_accessories |
| 6 | 1 | | category_name_women_bag |
| 7 | 1 | | category_name_women_shoes |
| 8 | 6 | | category_name_women_bag_name_handbag|
| 9 | 6 | | category_name_women_bag_name_hanger_bag |
| 10 | 6 | | category_name_women_bag_name_backpack |
| 11 | 4 | | category_name_women_clothing_bottoms|
| 12 | 4 | | category_name_women_clothing_dresses|
Thank you!