Help with explandable category tree
Posted on
16th Feb 2014 07:03 pm by
admin
I have the below query:
SELECT l1.id as lev1_id,l2.id as lev2_id,l3.id as lev3_id,l1.category AS lev1, l2.category as lev2, l3.category as lev3
FROM categories AS l1
LEFT JOIN categories AS l2 ON l2.parentCat = l1.id
LEFT JOIN categories AS l3 ON l3.parentCat = l2.id
WHERE l1.parentCat = NULL
Which outputs something like:
-------------------------------------------------------
lev1_id | lev2_id | lev3_id |lev1 | lev2 | lev3 |
------------------------------------------------------
1 | 4 | 7 |makeup | lipstick | wet look
1 | 4 | 8 |makeup | lipstick |matt
1 | 5 | 9 |makeup | mascara| black
2 | 11 | NULL|skincare|creame | NULL
3 | 12 | 13 |haircare |hairspray| Xtra Hold
I loop through the results and list all the categories and subcategories as below.
$prevCat1Name = '';
$prevCat2Name = '';
foreach ($all_cats as $cats) {
if ($cats['lev1'] !== $prevCat1Name) {
echo $cats['lev1'];
}
if (($cats['lev2'] !== $prevCat2Name)&&($all_cat['lev2'] !== NULL)) {
echo ' - ' .$cats['lev2'];
}
if (($cats['lev3'] !== NULL)&&($all_cat['lev2'] !== NULL)) {
echo ' -- ' . $cats['lev3'] . PHP_EOL;
}
$prevCat1Name = $all_cat['lev1'];
$prevCat2Name = $all_cat['lev2'];
}
How do I amend the above code so that i can expand / contract the sublevels (without having seperate queries for each level)?
302
50
Other php-forum
Cant display mysql data
Hey all,
I am learning php and my first goal is to create a simple CMS. At the moment I am st
background color imagefill
Hello
I would like to ask you why I see this square in red color just in my local xampp insta
please help
HTML Code:
Code: <span id="ctl00"><span>
controlling/creating accounts with privileges
hi all,
i'm trying to figure out how i can create/control account with privileges, for instance a
Referential Integrity problem when inserting into 2 tables at same time
Hello Everyone,
Im relativity new to PHP and MySQL and i have come up against a problem. i h
Required to login help
I'm trying to set up my site so users have to be logged into the forum to access the site.I've been
php unable to sent email
Hi, guys, I try to send a email through php but unfortunately the mail unable to sent. Here is my co
Cannot Connect to Database
I am writing an application to do annual reviews. I cannot get my script to work. Whenever it runs I
Form errors in an array
I'm processing a form and putting the errors in an array. empty($errors) doesn't seem to do the tric
problem when runing a funciton
I am trying to connect to database using a funciton in a functions.php
<?php
funct