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
Did you know?Explore Trending and Topic pages for more stories like this.
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)?
No comments posted yet

Your Answer:

Login to answer
302 Like 50 Dislike
Previous forums Next forums
Other forums

Upload Code Help
Hi everyone I need some help with a bit of code ive been working with for a while. I am completely s

Wrong Info sent Using $get in form
Hi i have this code that i tried to make to send some info from one page to aother with lots of ppls

Are sessions secure at all?..
I haven't really gotten into yet, but I was just thinking of something weird..

Lets say you h

Form always sends to error page...
Hello,
Any help will be greatly appreciated. I am having trouble getting multiple fields to be re

Inserting Data into a MS Access DB using PHP.
As part of my uni course I am doing a placement at a company whom want me to create a client zone fo

Newb advice
Hi all,

I'm a flash front end designer and I've taken on a project that needs some back end p

Disable Scrollbar in TableControl
Hello everbody,

please can anybody help me - outherwise i'm going to become desperate :P<

Baffled by Undefined Index in Simple Array: Please Help!
Hello. I have a form which posts an array to this script. However, I can't seem to access the values

php call servlet
I have done a php backup application .
So there is a form that user pick some files to zip and d

sql problems
I am having a small problem with my sql statement. it is inserting into 4 of the fields.

fie

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash