Forum Moderators: coopster
I am trying to make a FAQ section and I am stuck on building a category listing. I have a category listing for my products but I only built it to handle up to 2 sub categories. The FAQ section I will be building will go roughly 5 categories deep.
I am trying to build a script that will print off all the categories but for some reason I am having a hard time building it. I was curious if anyone could help. Perhaps give me a script that is useful and easy for me to take apart and learn, or even better a tutorial.
The way I have it working currently is I have 2 functions. One checks for a sub category and the next function prints it off. The problem I am having is making it loop until there are no more sub categories. I'm finding it even hard to explain. In my mom its simple how I want it to work but I'm still fairly new to PHP so its a matter of logic I guess.
Any help would be greatly appreciated. If you need any info please feel free to ask me and I will answer it to the best of my ability :)
Thanks.
Wes
The tree concept is easy: to build a tree, you need nodes. Each node has a parent. There is a root node which is the ancestor of everything. It's easy to construct one in a database:
Table "categories"
------------------
categoryID (int, auto primary)
categoryame (varchar)
categoryparent (int)
Each category is row in the table, a node on the tree, each node has a name and a parent. The "root" node should have a parent of -1. It's easy to create scripts that "walk" through the tree finding siblings, parents, children, etc. The structure is similar to what you'd see in an XML file.
Then you can have all your FAQs and assign them to a category, by the categoryID
table "FAQs"
------------
FAQID (int, auto primary)
FAQname
FAQsummary
FAQquestion
FAQanswer
FAQcategory (is one of the categoryID)
Recursion is a strategy that seems difficult at first, but becomes second-nature the more you use it. "While" and "For" loops are very useful, but sometimes they aren't sufficient. Recursion will "dig" into each level of a hierarchy and stop at the bottom, so it's ideal for displaying any kind of tree diagram, whether it's a file/directory system, a navigation hierarchy, or an arrangement of nested categories.
Here's a mediocre tutorial [codewalkers.com], and here's a better one [personal.vsnl.com].
[edited by: jatar_k at 4:11 pm (utc) on July 15, 2004]
[edit reason] removed example link [/edit]
thanks for the posts. if anyone else has anything to add please feel free, i can use all the help i can get ;)
wes
One way to do it is to have your categories in a database table. You would have these fields (first three two, the others just for an example):
cat_id
parent_id
cat_name
cat_dir
cat_url
Then when you build your category tree, you do it recursively, asking the DB whether the current category has any subcats. We begin with the id of 0, that is the top-level categories that have no parents.
$tree = build_tree(0);
function build_tree($id)
{
$tree = "";
$q = "SELECT cat_id FROM cats WHERE parent_id=$id ORDER BY cat_name";
$subcats = mysql_query($q);
if (mysql_num_rows($subcats)
{
$tree = "<ul>";
while ()
{
$tree .= "<li>"
. make_cat_link($id)
. build_tree($id)
. "</li>";
} // end while
$tree .= "<ul>";
} // end if
return $tree;
} // end func