Forum Moderators: coopster
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....
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.
<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>
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.
Do a Google search for it and see the Sitepoint article. It should suffice all your needs.
:)
You realize that a tree traversal is still recursive, right?