RE: [PHP-DB] Re: Alternating rows pulled from database

From: Date: Sat, 01 Dec 2001 03:57:56 +0000
Subject: RE: [PHP-DB] Re: Alternating rows pulled from database
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-14733@lists.php.net to get a copy of this message
I'm new to PHP, so I don't know if there are any substantive advantages in execution time to any particular method, but here's what I do (granted, I've never tried it in PHP, but I do it all the time in VBScript). $cols = array("#FFFFFF", "#EEFFEE"); for ($i=1; $i<=count(mysql_num_rows($return); $i++) echo "<tr bgcolor='" . $cols[$i % 2] . "'>"; //..etc next Don't crucify me if the syntax is wrong; hopefully you get the idea. I don't have PHP with me right now, so I won't be able to test it till next week. Would this actually work in PHP? The idea being that if you put it in an array and suddenly decided that you needed more than 2 colors, you just add your colors to the array and change the modulus number. Thoughts? TIM _____________________________________ l i n e a r c o n c e p t s http://www.linearconcepts.com > -----Original Message----- > From: Ryan Marrs [mailto:rmarrs@strtrade.com] > I don't know if this is a better way to do it, but here's how I do it: > > $return = mysql_query("SELECT This FROM That WHERE This = That"); > Print "<table> > For($i=1;$i<=count(mysql_num_rows($return);$i++) > If($i % 2 == 0){$color="color1";} else {$color="color2";} > > Print " <tr bgcolor='#$color'> ". > " <td>$value[$i]</td> ". > " </tr> " > ; > } > Print "</table> " > ; > > > > Here's what I did to get alternating row colors on a results > page... I'm > not > > > sure that I follow what you are asking, but I hope this will help you: > > > > > > > > > $color_one = "dddddd"; > > > $color_two = "cccccc"; > > > $row_count = 0; > > > // SEND QUERY > > > while (row = fetch array(result)) > > > { > > > $row_color = ($row_count % 2) ? $color_one : $color_two; > > > other fields from DB > > > echo "<tr bgcolor=$row_color">; > > > echo "<td>"; > > > echo data out > > > echo "</td>"; > > > echo "</tr>"; > > > $row_count++; > > > } > > > > > > > > > This is a really simple system. It uses the modulus (%) > operator. If the > > > result of the math is even it uses color_one otherwise it uses > color_two.. > > > > > > I found it somewhere, can't remember where right now.. But at least it > works > > > and it's only a few lines of code!! > > > > > <SNIP> > > > > Think this would work? > > > > $rowcount = 0 > > while($forum = $db->fetch_array($query)) { > > $forumlist .= ($row_count % 2) ? > > forum($forum, "index_forum") : > > forum($forum, "index_forum2"); > > $rowcount++; > > } > > > > > >

« previous php.db (#14733) next »