Re: Alternating rows pulled from database

From: Date: Thu, 29 Nov 2001 22:23:20 +0000
Subject: Re: Alternating rows pulled from database
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-14701@lists.php.net to get a copy of this message
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!! "Mrbaseball34" <mrbaseballthirtyfour@hotmaildot.com> wrote in message news:MPG.167010b0e0a4776298969b@216.92.131.4... > I am trying to modify the XMB Forums to be more like > UBB in looks. > > In this code, I want to alternate forum_index templates. > > I have created two templates, one using $altbg1 and one using $altbg2. I want > to alternate the row colors... > > How would I accomplish that? > {from: index.php} > > code: > while($group = $db->fetch_array($queryg)) { > if($group[type] == "group") { > eval("\$forumlist .= > \"".template("index_category")."\";"); > if($catsonly != "on" || $gid) { > $query = $db->query("SELECT * FROM $table_forums WHERE type='forum' > AND status='on' AND fup='$group[fid]' ORDER BY > displayorder"); > while($forum = $db->fetch_array($query)) { > $forumlist .= forum($forum, "index_forum"); > } > } > }else { > $forumlist .= forum($group, "index_forum"); > } > } > > > What I really need to do is something like this: > > $i = 0 > while($forum = $db->fetch_array($query)) { > if ($i % 2 = 0 { > $forumlist .= forum($forum, "index_forum"); > } else { > $forumlist .= forum($forum, "index_forum2"); > } > $i = $i + 1; > }

« previous php.db (#14701) next »