Forum Moderators: coopster

Message Too Old, No Replies

Logic behind unlimited categories?

Trying to figure out how to make a script with unlimited categories...

         

amnesia440

4:25 am on Jun 8, 2008 (gmt 0)

10+ Year Member



So I will be working on a script in which I will need to have unlimited categories and subcategories.

For example, a script that is a photo album would have a main album, and then an unlimited amount albums below that and those albums could have albums in them also. I know I'm being vague, my apologies.

So I am trying to figure out the logic behind this. I know that I have to assign a couple id's to each categoy. First id is the id for the category and the second id is the id of the parent category.

But then how do I distiguish 'top level' categories?

then, after all this...how do I use this information to make a 'breadcrumb?'

Any links or information would be great....

PHP_Chimp

11:18 am on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you going to be using a database for this, or are you using a flat file?

Using a database I have a similar arrangement.

CREATE TABLE post (
post_id serial,
date_added timestamp DEFAULT now() NOT NULL,
member_id int NOT NULL,
post text NOT NULL,
reply_id int DEFAULT 0NOT NULL,
CONSTRAINT post_pk PRIMARY KEY (post_id),
CONSTRAINT post_post_empty CHECK (post <> '')
);
-- select main posts
-- main posts have a reply_id of 0
SELECT * FROM post WHERE reply_id = 0;

Im not sure if this logic is actually that good, however it works.
It would probably be better form to separate the main album from all sub albums. Then use the main_album_id as a foreign key on the other table.
/* I'm just to lazy to do that ;) */

You may actually want to ask the SQL [webmasterworld.com] people if you are using a database.

If you are using mysql then you will need to adjust the code slightly, as that isnt code from mysql...but I'm sure you can work it out.

henry0

12:22 pm on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like to enter in the DB a few fields such as:
main_cate
sub_cate
gal_name/thumbs_name/ (a gallery dir name and thumbs dir name in each sub_cate)
then when your gal is created(MKDIR)
enter in the DB
-another field- gal_dir (something like: $main_cate/$sub_cate/$gal_name/$thumbs_name/$thumbs/$thumb(example: $thumb could be 1.jpg)
the rest is up to you......

<edit>I suppose that when creating a new gal you are saving the original copy in a tmp, save a copy as is or resized/optimized in /gal/ and create a thumb in gal/thumbs/
</edit>

cameraman

5:09 pm on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



'unlimited' implies a recursive function (a function that calls itself).

You're right, you want an id for the category and another id for its parent. Use an 'invalid' id for top level categories. That is, if your very first category is id number 1, then use zero for top. If your first category is zero, then use -1 to signify top.

I also like to have a flag telling me whether the category has children, but that's not strictly necessary - if your recursive function returns nothing for the specified category then you know it doesn't have children.
This pseudocode will get your category hierarchy into an array:
function recursive($cat_id) {
$items = array();
$idx = 0;
$qry = query("* where parent=$cat_id");
while($dat = fetch($qry)) {
if($dat['have_children'])
$items[$idx]['children'] = recursive($dat['cat_id']);
$items[$idx]['data'] = $dat;
$idx++;
} // EndWhile getting data
return($items);
} // End recursive()

$content = recursive(-1);

To do breadcrumbs, you work backward. You can either carry the crumb path into a recursive function or use a global:
function crumby($cat_id) {
global $path;
$qry = query("title,parent_id where cat_id=$cat_id");
$dat = fetch($qry);
free_result($qry);
array_unshift($path,$dat['title']);
if($dat['parent_id'] > -1)
crumby($dat['parent_id']);
}
$path = array();
crumby($target_id);

Recursive functions can quickly get resource intensive. See how I free the query resource in the second function? If the number of levels you have grows large (what number 'large' is is up to you) you would also want to restructure recursive() to do the same thing; you'd need to read the entire current level into a variable and free the resource before you process the children - that's when 'have_children' gets handy to have available.

ashishp

4:24 am on Jun 9, 2008 (gmt 0)

10+ Year Member



While recursion is usually used it is also CPU intensive and slow. There is another method for creating categories to an unlimited depth and an easy way to make bread crumbs from the current category - Modified Preorder Tree Traversal Method.

Do a Google search for it and see the Sitepoint article. It should suffice all your needs.

:)

eelixduppy

3:04 pm on Jun 9, 2008 (gmt 0)



>> Modified Preorder Tree Traversal Method.

You realize that a tree traversal is still recursive, right?

ashishp

8:29 am on Jun 12, 2008 (gmt 0)

10+ Year Member



Actually, I didnt! My bad. :(

But perhaps this model is lighter on the CPU. (in uncharted waters here....)

Well atleast it is an option. :)