Page 1 of 1

format tree traversal

Posted: Sat Apr 21, 2007 8:56 pm
by GeXus
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm using the tree traversal method for storing categories, what I can't figure out is how do I format it appropriatly in the php.. here is what I have for mysql

[syntax="sql"]
SELECT CONCAT( REPEAT( ' ', (COUNT(parent.name) - 1) ), node.name) AS name
FROM categories AS node,
categories AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;
So you can see that each new node will have a space before it, indenting each one accordingly. I don't want to do this in sql however. I want to be able to format this in php... any ideas how I would do that?

Thanks a lot!


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Apr 21, 2007 10:15 pm
by volka
You can SELECT COUNT(parent.name) - 1 as level, node.name and then use the field level to format the output in php.

Posted: Sun Apr 22, 2007 1:12 pm
by GeXus
perfect, thanks!

Posted: Sun Apr 22, 2007 2:45 pm
by GeXus
Okay, I'm confused... This is what I have...

Code: Select all

$deep = 1;
while($row = $query->fetch_array(MYSQLI_ASSOC)){
	if($row['deep'] > $deep){

		echo "<a href=?cat_id=" . $row['category_id'] . ">" . $row['name'] . "</a> [ <a href=\"?delete_cat_id=" . $row['category_id'] . "\">x</a> ]<br/>";
		
	}
	elseif($row['deep'] = $deep){
		
		echo "--<a href=?cat_id=" . $row['category_id'] . ">" . $row['name'] . "</a> [ <a href=\"?delete_cat_id=" . $row['category_id'] . "\">x</a> ]<br/>";	
		
	}else{
			
		echo "<a href=?cat_id=" . $row['category_id'] . ">" . $row['name'] . "</a> [ <a href=\"?delete_cat_id=" . $row['category_id'] . "\">x</a> ]<br/>";	
	}
	
	$deep = $row['deep'];
}
But this definitly is not right... would you mind explaining how I would go about doing it?

Posted: Sun Apr 22, 2007 6:38 pm
by Weirdan

Code: Select all

elseif($row['deep'] = $deep){
Are you sure you really wanted an assignment in there?

Posted: Sun Apr 22, 2007 6:44 pm
by GeXus
Yeah, actually I fixed that.. but the whole thing is just not right...