PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Listing categories and sub-categories in tree format

New Topic-Reply




Author
Post
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 Mon Aug 16, 2010 06:28:36
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:
Code:

<?php
print "<select name='category'>";
$selcat="SELECT * from cl_categories order by CatName ASC";
$selcat2=mysql_query($selcat) or die("Could not select category");
traverse(0,0,$selcat2);
print "</select><br>";
?>


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:

Code:

<?php
function traverse($root, $depth, $sql)
{
$row=0;
while ($acat = mysql_fetch_array($sql))
{
if ($acat['CatParent'] == $root)
{
print "<option value='" . $acat['CatID'] . "'>";
$j=0;
while ($j<$depth)
{
print "&nbsp;&nbsp;";
$j++;
}
if($depth>0)
{
print "-";
}
print $acat['CatName'] . "</option>";
mysql_data_seek($sql,0);
traverse($acat['CatID'], $depth+1,$sql);

}
$row++;
mysql_data_seek($sql,$row);

}
}
?>


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 (&nbsp) 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?

Code:

// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name = $value['name'];
$children = $value['children'];

$list .= "<optgroup label=\"$name\">";

foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";

if ($child['id'] == $cat_id) {
$list .= " selected";
}
$list .= ">{$child['name']}</option>";
}

$list .= "</optgroup>";
}


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,

sciscov
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 22914
[PM sciscov]

RPS score: 0
RPS challenge

Posted at Tue Jun 09, 2009 13:07:12
Edit post|Quote
Ok Friend, i have test it in my local computer. this function not have a problem but when i try in the internet it be like this :Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 17 is invalid for MySQL result index 23 (or the query data is unbuffered)
-----------------------------
Thank Friend

AzeriFire
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 23432
[PM AzeriFire]

Posted at Tue Jul 07, 2009 18:50:09
Edit post|Quote

after $row++ just replace mysql_data_seek($sql,$row) to            if(mysql_num_rows($sql)!=$row){            mysql_data_seek($sql,$row);}
-----------------------------
My Mobile phones catalog CMS

PHP_Adam
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 23531
[PM PHP_Adam]

RPS score: 0
RPS challenge

Posted at Mon Jul 13, 2009 08:33:21
Edit post|Quote
This is exsactly what I was looking for, except I receve many of these errors:Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 13 is invalid for MySQL result index 8 (or the query data is unbuffered) in D:\wamp\www\ECMS\inc\func_essential.php on line 27Any ideas?
-----------------------------
PHP_Adam

tomhick
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 24636
[PM tomhick]

RPS score: 0
RPS challenge

Posted at Mon Oct 05, 2009 01:00:45
Edit post|Quote
A node is a structure which may contain a value, a condition, or represent a separate data structure (which could be a tree of its own). Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees grow down, not up as they do in nature). A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent. Nodes always have references to their children, but not always to their parent. Nodes that do not have any children are called leaf nodes. They are also referred to as terminal nodes.The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree. The depth of a node is the length of the path to its root (i.e., its root path). This is commonly needed in the manipulation of the various self balancing trees, AVL Trees in particular. Conventionally, the value -1 corresponds to a subtree with no nodes, whereas zero corresponds to a subtree with one node.
-----------------------------
generic cialis - new dvd releases this month - mens watches

alice
Rank:acorn
Group: members
Posts: 31
IP Logged
PM ID and RPS ID: 30842
[PM alice]

RPS score: 0
RPS challenge

Posted at Thu Jul 15, 2010 02:22:46
Edit post|Quote




Thanks for sharing this post. iseb
certification
This is a very helpful and informative
material.
ccnp Good post and keep it up. Websites are always
helpful in one way or the other, that’s cool stuff,
ccna dumps anyways, a good way to get started to renovate your
dreams into the world of reality.I will write more in detail after my
SY0-201 very soon. Thanks



  





ping123
Rank:acorn
Group: members
Posts: 10
IP Logged
PM ID and RPS ID: 31806
[PM ping123]

RPS score: 0
RPS challenge

Posted at Mon Aug 16, 2010 06:28:36
Edit post|Quote
A node is a structure which may contain a value, a condition, or represent a separate data structure (which could be a tree of its own). Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees grow down 70-663 dumps, not up as they do in nature). A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent. Nodes always have references to their children, but not always to their parent. Nodes that do not have any children are called leaf nodes. They are also referred to as terminal nodes 70-663 exam.The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree. The depth of a node is the length of the path to its root (i.e., its root path). This is commonly needed in the manipulation of the various self balancing trees, AVL Trees in particular. Conventionally, the value -1 corresponds to a subtree with no nodes, whereas zero corresponds to a subtree with one node 70-663 questions.

Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York