Jump to content

[SOLVED] changes in bg color for repeating table


confused_aswell

Recommended Posts

Hi

 

Does anyone know how I might configure the while loop to have alternate background colors from the database results?

 

I would be greatful of any starting points or tutorials or some coding please.

 

Thanks,

 

Phil

 

<table align="center"  border="1" >
<?php 
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) { ?>    

<tr><td><?php echo  $list['orders_id'] . " , " . $list['customers_name'] . " , " . $list['customers_street_address'] . " , " . $list['customers_suburb'] . " , " . $list['customers_city'] . " , " . $list['customers_state'] . " , " . $list['customers_postcode'] ."<br />"; 
}  ?></td>
</tr>
</table> 

 

Link to comment
Share on other sites

This have been covered quite a few times So heres Very Quick example

 

<?php
$A = array("One", "Two", "Three");
echo "<table>";
$col[0] = "#FF0000"; //Red
$col[1] = "#0000FF"; //Blue
$c =0;
foreach($A as $R)
{
$c = ($c==1)?0:1;
echo "<tr bgcolor=\"{$col[$c]}\">";
echo "<td>$R</td>";
echo "</tr>";
}
echo "</table>";
?>

Link to comment
Share on other sites

<table align="center"  border="1" >
<?php 
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) {   
$color = ($color == '#FF0000')? '#0000FF' : '#FF0000'; 
?>
<tr><?php echo "<td bgcolor='$color'>" . $list['orders_id'] . " , " . $list['customers_name'] . " , " . $list['customers_street_address'] . " , " . $list['customers_suburb'] . " , " . $list['customers_city'] . " , " . $list['customers_state'] . " , " . $list['customers_postcode'] ."<br />"; 
}  ?></td>
</tr>
</table> 

Link to comment
Share on other sites

Hi

 

All of the rows are blue, is that supposed to be like that?

 

Thanks

 

Phil

<table align="center"  border="1" >
<?php 
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) { ?>   
<?php $color = ($color = '#FF0000')? '#0000FF' : '#FF0000'; ?>
<tr><?php echo "<td bgcolor='$color'>" . $list['orders_id'] . " , " . $list['customers_name'] . " , " . $list['customers_street_address'] . " , " . $list['customers_suburb'] . " , " . $list['customers_city'] . " , " . $list['customers_state'] . " , " . $list['customers_postcode'] ."<br />"; 
}  ?></td>
</tr>
</table> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.