confused_aswell Posted October 17, 2008 Share Posted October 17, 2008 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> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 17, 2008 Share Posted October 17, 2008 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>"; ?> Quote Link to comment Share on other sites More sharing options...
confused_aswell Posted October 17, 2008 Author Share Posted October 17, 2008 Hi Thanks for the quick reply, I am not so good with raw coding, so will this script intergrate with my script easily? Thanks, Phil Quote Link to comment Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 <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> Quote Link to comment Share on other sites More sharing options...
confused_aswell Posted October 17, 2008 Author Share Posted October 17, 2008 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> Quote Link to comment Share on other sites More sharing options...
confused_aswell Posted October 17, 2008 Author Share Posted October 17, 2008 Hi Don't worry, I just copied it wrong. Thanks very much for your help. Cheers, Phil Quote Link to comment Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 You must have copied it before I edited it. I accidentally put = instead of == in the condition. Quote Link to comment Share on other sites More sharing options...
confused_aswell Posted October 17, 2008 Author Share Posted October 17, 2008 I thought that was funny, anyway, thanks for that! Have a nice day, Phil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.