Faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 16 people (88%) answered Yes
Recently 7 of 8 people (88%) answered Yes

Entry

Looping through an array to populate a table from a database, I wish to test the values and change the color of the tag accordingly....

Apr 23rd, 2000 17:26
Espen Holje, Stephanie Lemaire, www.weberdev.com


Try this:
1. Fetch data from database:
$query = "select * from table";
$result = MYSQL_QUERY($query) or die("Database error");
$number = MYSQL_NUMROWS($result);
2. Loop through results and flip colors
IF ($number > 0)
{
WHILE ($i < $number):
$text = mysql_result($result,$i,"opprettet");
$bgColor = "#C0C0C0";
$i % 2 ? 0: $bgColor = "#E8E8E8";
print "<tr>\n";
print "<td width=\"25%\" bgcolor=\"$bgColor\">" . $text . "</td>\n";
print "</td>\n";
print "</tr>\n";
$i++;
ENDWHILE;
}