format tree traversal

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

format tree traversal

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

perfect, thanks!
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

elseif($row['deep'] = $deep){
Are you sure you really wanted an assignment in there?
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Yeah, actually I fixed that.. but the whole thing is just not right...
Post Reply