|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Listing categories and sub-categories in tree format | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Wed Jul 30, 2008 07:15:19 Edit Post|Quote This tutorial will show you the basics of recursion abd how to list categories and sub-categories to the nth level in tree format in a directory kind of script. For this example, we will need a SQL table with the following: A table called cl_categories with these fields. 1.CatID which is a bigint, primary, and auto-increment 2. CatName, which is varchar(255). This is the name of the category 3. CatParent, a bigint, which is the ID of the Parent category, if this is a sub-category. If CatParent is 0, then it signifies that the category is a top-level category. To display these categories in a drop down box, we first need a select form:
Transverse is the actual function that is going to generate the tree structure for our categories and sub-categories. We initially pass it values of 0,0,and $selectcat2 for its root, depth, and the actual query data. We pass is 0 and 0 for root and depth because initially we want to start from top level directories. Now here the actual function:
This is pretty complicated. Basically its going through all the categories in the while loop and seeing if the current categories is equal to root. Since initially the root is zero, it just prints the category without indentations. It also sets a variable, j, to keep track of how deep in the tree a category is, so it can print the appropriate number of indentations ( ) segments so the tree will look right. Also if the depth of the categories is not zero,meaning its a subcat, it will print a dash indicating its a subcat of a higher level category. Then the function calls itself and adds one to the depth only if the Catparent is not zero. This way, it will keep transversing the nodes of the tree until it goes through all the categories and subcategories. Calling itself and incrementing the depth by 1 ensures proper level of the category within the tree. When the while loop finishes, you will have all the categories displayed in tree format. The MySQL_data_seek function is there to rebuffer the query so you can go through it again at the end of each iteration. ----------------------------- Chipmunk, Supreme Administrator | ||
| rem Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 11681 [PM rem] RPS score: 0 RPS challenge | Posted at Thu Feb 01, 2007 21:21:31 Edit post|Quote This is a great tutorial but unfortunately it gives an error! Try it out without using the lists or look at the HTML source code ;) Do you think you could re-post this without the error? I tried to fix it but didn't succeeded... The error is: Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 6 is invalid for MySQL result index 4 (or the query data is unbuffered) in .... etc ... on line 33 ----------------------------- -- Rem | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Thu Feb 01, 2007 23:29:17 Edit post|Quote It'll give you that error only if you don't already have a populated list of MYSQL categories. Since its listing the categories in tree format with mySQL categories, this tutorial is no really relevant if you don't already have a populated mySQL table of categories/sub-categories. Alternativey if you don't want that error to appear when a table is unpopulated. You could check if mysql_num_rows($sql) is zero or not and only run mysql_data_seek if its not zero. Note on the tutorial: I've only gotten it to work as a dropdown list. I doesn't quite function right if you try to printing it out otherwise. ----------------------------- Chipmunk, Supreme Administrator | ||
| ithapiri Rank:acorn Group: members Posts: 9 IP Logged PM ID and RPS ID: 1380 [PM ithapiri] | Posted at Sat Feb 03, 2007 00:36:43 Edit post|Quote see in this code add @ in front of the mysql_data_seek($sql,$row); so it looks like @mysql_data_seek($sql,$row); it supresses the error shows above ....is it correct dear admin:cool | ||
| hugoman Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 11729 [PM hugoman] RPS score: 0 RPS challenge | Posted at Mon Feb 12, 2007 11:52:00 Edit post|Quote Hello, thats my first post... ,the script is that what i have searched over the internet, but how to make it to display the categries in tree, <ul><li>.... Thanks ;) ----------------------------- Hello boys and girls... nice forum )) | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Mon Feb 12, 2007 23:08:49 Edit post|Quote Try just printing the entries instead of having them in a <select> option box. ----------------------------- Chipmunk, Supreme Administrator | ||
| Moogie17 Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 11938 [PM Moogie17] RPS score: 0 RPS challenge | Posted at Wed Mar 14, 2007 17:34:38 Edit post|Quote Thanks for this great bit of code, it's almost exactly what I'm looking for. Don't mean to be cheeky, but can anyone here tell me how I can get this to just output the contents to a variable instead of printing it immediately? I've tried various things, but only seem to get one level returned when I try to do it. No doubt someone here knows rather more PHP than I and could assist! Many thanks :D ----------------------------- Moogie | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Wed Mar 14, 2007 19:37:29 Edit post|Quote You'd probably have to store it in a array with the following structure: $array[nodedepth][node]=value Thats the only way I could see to successfully store the structure. ----------------------------- Chipmunk, Supreme Administrator | ||
| Moogie17 Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 11938 [PM Moogie17] RPS score: 0 RPS challenge | Posted at Thu Mar 15, 2007 14:22:51 Edit post|Quote Thanks very much! I'm not very good with arrays, but I'll keep tinkering and will probably get it working All the best! Moogie ----------------------------- Moogie | ||
| BilltheCat Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 14279 [PM BilltheCat] RPS score: 0 RPS challenge | Posted at Mon Nov 26, 2007 02:13:54 Edit post|Quote Hi, I'm trying to take what you have here use it in a shopping cart where I add the products. The old version doesn't allow more than one subcategory, and I like the idea that yours allows unlimited. But the old one does one thing this does not, and that is keeping the category location when modifying products. If I add a product, then go back and edit it, I can't tell from this script exactly which category it was originally - and it saves in root if I don't change it. Any ideas? This is where the old script finds the option to maked "selected", how can I integrate this with yours?
| ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Wed Nov 28, 2007 16:34:19 Edit post|Quote You have to select the product you are trying to modify, get its category, and insert it as the default option. On the modify file, there should be an ID or something that identifies the product you are trying to modify. ----------------------------- Chipmunk, Supreme Administrator | ||
| al3loo Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 16847 [PM al3loo] RPS score: 0 RPS challenge | Posted at Wed Jul 30, 2008 07:15:19 Edit post|Quote Hi Everyone, I like this code and this site .. but I want to ask how could I make it select the option direct. i.e: If I in the admin and want to edit cat so its should select the current category before I edit it, I think its an (if condition) but I don't know how to use it ! ((selected="selected")) Also, I am asking If somebody could make a code to show the cat and subcat like this: Homepage >> category >> subcat .. Thanks in advanced. Regards, | ||
Page: 1 |