RE: [PHP] Re: Need row number from database select

From: Date: Sun, 14 Apr 2002 22:49:48 +0000
Subject: RE: [PHP] Re: Need row number from database select
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-92941@lists.php.net to get a copy of this message
In article <NDBBJHKAELJNNJMBMFHDMEECCOAA.internetracer@yifan.net>, internetracer@yifan.net says... > I am displaying a folder tree and want to alternate the colors for each > folder so my database table shows that for example the Apples folder is > located under the Fruits folder. Therefore I want to know which row is odd > or even so I can assign it different colors. Incidently, does anyone know > of a more efficient way of displaying a folder tree without doing it > recursively? > > id parent_id name > == ========= ======= > 1 1 Home > 2 1 Products > 3 2 Fruits > 4 2 Vegetables > 5 3 Apples > > > function build_folder_tree(&$output, $parent=1, $indent="") > { > $qid = mysql_query("SELECT id, name FROM folders WHERE parent_id = > $parent"); > > while ($cat = mysql_fetch_object($qid)) > { > $output .= $cat->name + $indent; // here is where I want to assign diff. > colors > if ($cat->id != $parent) > build_folder_tree($output, $cat->id, $indent."&nbsp;&nbsp;"); > } > } Well, you don't need to know what row number is. Something like: function build_folder_tree(&$output, $parent=1, $indent="") { $color = "red"; // Set var to first option $qid = mysql_query("SELECT id, name FROM folders WHERE parent_id = $parent"); while ($cat = mysql_fetch_object($qid)) { $output .= $cat->name + $indent; // here is where I want to assign diff. colors $color == "red" ? $color = "blue": $color = "red"; // Cycle color for each iteration if ($cat->id != $parent) build_folder_tree($output, $cat->id, $indent."&nbsp;&nbsp;"); } } -- David Robley Temporary Kiwi! Quod subigo farinam

« previous php.general (#92941) next »