Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alternating table row color

Status
Not open for further replies.

yerfdog

Technical User
Sep 8, 2002
2
US
Does anyony know if you can tell a query table to alternate the color of every-other row? So, for example the odd numbered rows would be white and the evens gray. If so, how?

yerfdog
 
Something like this?

Code:
<?php
print '<html><head><style>
        .gray { background-color: gray; };
        .white { background-color: white; };
</style></head><body><table border=1>';

$dbh = mysql_connect ('localhost', 'fu', 'bar');
mysql_select_db('fubar', $dbh);
$rh = $mysql_query (&quot;SELECT fu, bar from fubar&quot;, $dbh);

$counter = 0;
while ($result = mysql_fetch_array ($rh))
{
        print '<TR class=';
        print ($counter % 2 == 0)? &quot;gray&quot; : &quot;white&quot;;
        print '><td>$result['fu']</td><td>$result['bar']</td></tr>';
        $counter++;
}

print '</table></body></html>';
?>
______________________________________________________________________
TANSTAAFL!
 
OK, sorry it took so long to get back to you, but I got swamped.

I tried experimenting with what was provided above, but I'm very new at php (teaching myself for 2 weeks) and I'm not sure how to incorporate what's above with what I have that is currently working below.

<?php
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo &quot;<p align=center><b>You have not entered search details. Please go back and try again.</b></p>&quot;;
exit;
}
/* Connecting, selecting database */
@ $db = mysql_connect(&quot;db_host&quot;, &quot;db_user&quot;, &quot;db_pass&quot;)
or die(&quot;Could not connect&quot;);
mysql_select_db(&quot;db&quot;) or die(&quot;Could not select database&quot;);

/* Performing SQL query */
$query = &quot;SELECT * FROM table WHERE &quot;.$searchtype.&quot; LIKE '%&quot;.$searchterm.&quot;%' order by 'name' asc&quot;;
$result = mysql_query($query) or die(&quot;Query failed&quot;);
$num_results = mysql_num_rows($result);
print &quot;<p><b>Your search produced &quot;.$num_results.&quot; matches.</b></p>&quot;;
/* Printing results in HTML */
print &quot;<table border=\&quot;1\&quot; cellspacing=\&quot;0\&quot; cellpadding=\&quot;4\&quot; width=\&quot;500\&quot; align=\&quot;center\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td bgcolor=#0099CC colspan=\&quot;3\&quot; width=\&quot;50%\&quot; align=\&quot;center\&quot;><p><b>Organization</b></p></td>\n&quot;;
print &quot;<td bgcolor=#0099CC colspan=\&quot;5\&quot; align=\&quot;center\&quot;><p><b>Information</b></p></td>\n&quot;;
print &quot;</tr>\n&quot;;
while ($row = mysql_fetch_assoc($result)) {
print &quot;<tr>\n&quot;;
printf(&quot;<td colspan=\&quot;3\&quot;><p><b>%s</b><br> %s<br> %s</td><td align=\&quot;right\&quot; colspan=\&quot;5\&quot;><p>%s<br> %s, %s. %s<br> %s</td>\n&quot;, $row['Name'], $row['Gender'], $row['Affiliation'], $row['Address'], $row['City'], $row['State'], $row['Zip'], $row['Phone']);
print &quot;\t</tr>\n&quot;;
}
print &quot;</table>\n&quot;;

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($db);
?>

yerfdog
 
The core of the idea is to maintain a counter. As you output each row to check whether that counter is an even multiple of 2.

&quot;$counter % 2&quot; uses the modulus operator &quot;%&quot;. Modulus returns the remainder of division. If &quot;$counter % 2&quot; is zero, the value in $counter is even.

Then use the result of the modulus to determine whether to set the table row's class to &quot;gray&quot; or &quot;white&quot;.

The two classes are defined in the &quot;<style>&quot; section output at the top of the script. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top