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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Records from recordset every other record with different backg

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Greetings,

I currently have a table that displays all records in the recordset, Trying to figure out how to have every other record with a green background. This is what i currently have:

<?php while ($row_GetAlert = mysql_fetch_assoc($GetAlert)) { ?>
<p><?php echo $row_GetAlert['AlertDescription']."<br>\n"; ?></p><td width="117" valign="top"><div align="right">
<p><?php echo $row_GetAlert['AlertETR']."<br>\n"; } ?></p>

Any help is apprecaited. Running on a linux/mysql/apache combo.

Thanks
 
I would recommend the following setup, if a simple table layout is there
Code:
<?php 
$rowCount = 2;
$colors[0] = '#000000'; # white
$colors[1] = 'green';
echo '<table>';
while ($row_GetAlert = mysql_fetch_assoc($GetAlert)) { 
   $color = $colors[$rowCount%2];
   echo "<tr bgcolor='row$color'>";
   echo "<td>";
   echo $row_GetAlert['AlertDescription']."</td>\n";  
   echo '<td>';
   echo $row_GetAlert['AlertETR']."</td>\n";
   echo '</tr>';
   $rowCount++;
}
echo '</table>';

Code not tested. Ideally I would recommend to have a CSS stylesheet that has 2 styles defined and that are referenced with a class attribute in the <tr> tag.

The principle is the following:
$colors holds the two possible formatting data.
$rowCount is a counter that stars at 2 and is incremented
$color is calculated with the modulus function:
Counter mod2
2 0
3 1
4 0
5 1
etc. Every even row will be colors[0], all odd rows colors[1]. How you write out the HTML is up to you.
My example uses the bgcolor attribute which is obsolete and should not be ised - it is just an illustration.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top