Forum Moderators: coopster

Message Too Old, No Replies

FAQ's Category Listing

Making it search for endless subcategories

         

wfernley

1:46 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello :)

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

httpwebwitch

2:50 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are uncertain about the levels of categories and sub-categories you might run into... You will probably need to familiarize yourself with recursion, and how to build a hierarchical tree.

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]

charlier

2:55 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



Or if your lazy :-) you might just want to check out the
PHP/Scripts_and_Programs/FAQ_and_Knowledgebase/

category at hotscripts, they list 69 FAQ and Knowledgebase PHP
scripts.

wfernley

3:05 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



great thanks for the posts. yeah i am lazy but to a certain extent. for example im too lazy to write capitals in this post. but as for coding php i really want to know how it works inside and out. i dont like using other peoples scripts - i like to use my own which i write with other peoples scripts in mind. that way if something goes wrong its easier to debugg because i built it and i understand it better :)

thanks for the posts. if anyone else has anything to add please feel free, i can use all the help i can get ;)

wes

ergophobe

3:06 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

ergophobe

3:12 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahh. httpwebwitch always seems to out type me by a few minutes. I hope my late contribution is still useful in some way!

Tom

httpwebwitch

5:29 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do I hear an echo... (httpwebwitch smacks head thrice)
lol

ergophobe, we need a system where we can "call it" i.e. "I'll take this one, hwitch", "be my guest, ergo, I'll get the next one", ...

ergophobe

5:55 pm on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note to self: refresh thread before posting.