By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,708 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,708 IT Pros & Developers. It's quick & easy.

category management

omerbutt
100+
P: 638
hi can any one guide me how to control the categories in php as i have to make a vertical menu in php to show all the categories and subcategories inline to their parent
for eg. i have 3 main categories
Ladies
Men
Kid
then in each of the above categories i have to define the subcategories i.e.
tops
hoods
tees
sweat shirts
zipper
i have made the 2 tables in the databases which are as follows
ksl_categories
ksl_categories_description

ksl_categories
Field Type
cat_id int(11)
cat_image varchar(64)
parent_id int(11)
sort_order int(3)
date_added int(11)
last_modified int(11)

ksl_categories_description
Field Type
cat_id int(11)
lang_id int(11)
cat_name varchar(32)

can any one guide me in this regard and also that are these tables valid and correct to control the hierarchy
regards,
Omer Aslam
Feb 13 '09 #1
Share this Question
Share on Google+
6 Replies


xaxis
P: 15
Perhaps you can be more specific? For instance, what do you mean by "control"?
Feb 13 '09 #2

omerbutt
100+
P: 638
@xaxis
i have to show them in a hierarchical format means i have to make a vertical menu in which all the top level categories must be appearing in the 1st level i.e.
Ladies
Gents
Kids
then if i hover Ladies It must Show the Ladies Subcategories i.e.
Ladies Tees
Ladies Tops
Ladies Hoods
Ladies Zippers
Ladies Sweat Shirts

the client has asked me that there can be any number of subcategories means
he should be able to define another subcategory in the
Ladies Tees lets say he defines 2 more subcategories as
Printed Tees
Plain Tees
and then on clicking the plain tees all the plain tees must be sorted out of the databases
Feb 13 '09 #3

P: 88
For display purposes you need to look at a recursive function which Starts at the top and builds all the text throughout the unlimited of sub-categories. So if you had this real basic database design:

(CategoryID - ParentID - Name)

1 - 0 - Animals
2 - 1 - Dogs
3 - 1 - Cats
4 - 2 - Yorkshire Terrier
5 - 2 - Dalmation

So it would look something like this:

Expand|Select|Wrap|Line Numbers
  1. Animals
  2.    -- Dogs
  3.      -- Yorkshire Terrier
  4.      -- Dalmation
  5.    -- Cats
There may be another way but I had to do something like this before and I found a recursive function worked OK. Have you ever wrote a recursive function before?
Feb 13 '09 #4

omerbutt
100+
P: 638
@hoopy
yeah i do have but i am really stucked at this part can you give me a little hint for that any url or something
regards,
omer
Feb 14 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
must the data for the menu come from a database or will an XML file work too?
Feb 14 '09 #6

P: 88
OK I have put something together, I dont think its terribly efficient though. As it performs maybe too many queries if you had a mass of categories and subs. My tables looks like this for testing purposes:

Expand|Select|Wrap|Line Numbers
  1. cat_id     cat_parent     cat_name
  2. 1     0     Animals
  3. 2     1     Cats
  4. 3     1     Dogs
  5. 4     3     Yorkshire Terriers
  6. 5     3     Dalmations
  7. 6     1     Elephants
  8. 7     0     Cars
  9. 8     7     Sports Cars
  10. 9     7     Classic Cars
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. function cat_tree($parent,$level)
  3. {
  4.     $Q = mysql_query("
  5.       SELECT cat_id, cat_parent, cat_name
  6.       FROM categories 
  7.       WHERE cat_parent = '$parent' 
  8.       ORDER BY cat_name 
  9.     ") or die(mysql_error());
  10.     if(mysql_num_rows($Q) != 0)
  11.     {
  12.         while($R = mysql_fetch_array($Q))
  13.         {
  14.             for($x=0;$x<=$level;$x++) 
  15.                echo("&nbsp;&nbsp;&nbsp;");
  16.             echo('- ' . $R['cat_name'] . "<br />");
  17.             cat_tree($R['cat_id'], $x);
  18.         }
  19.     }
  20. }
  21.  
  22. cat_tree(0,0);
  23. ?>
So it starts with 0 cats i.e mains with no parents. And works its way through to the end. It produces a list like this:

Expand|Select|Wrap|Line Numbers
  1.    - Animals
  2.       - Cats
  3.       - Dogs
  4.          - Dalmations
  5.          - Yorkshire Terriers
  6.       - Elephants
  7.    - Cars
  8.       - Classic Cars
  9.       - Sports Cars
As I say there is probably a better way but its a start for you to build on. I guess it would be quicker to pull all the data into an array with one query then sort it out from there rather than multiple database calls. There is a guy who wrote a nice recursive function on some other recent thread so he may have a better solution. My SQL is not too grand so maybe you can achieve all of this with some sort of multiple join infested single query as well.

Good Luck.
Feb 14 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.