feri_soft Posted January 4, 2007 Share Posted January 4, 2007 Have you seen it on websites where results are listed with alternative colors for example one row is grey the next is blue and again and again changing. I cant find a way using while and fetch array to do this.Here is the result i want:[table][tr][td]1.[color=red]asdasdasd[/color][/td][/tr][tr][td]2.[color=bleu]bbbbbbb[/color][/td][/tr][tr][td]3.[color=red]cccccc[/color][/td][/tr][tr][td]4.[color=bleu]ddddddd[/color][/td][/tr][tr][td]5.[color=red]eeeeeee[/color][/td][/tr][tr][td]6.[color=bleu]ffffffffff[/color][/td][/tr][/table]But instead of changing the color of the text i will change the background,but i suppose the idea is the same. Link to comment https://forums.phpfreaks.com/topic/32833-multiple-row-colors/ Share on other sites More sharing options...
Orio Posted January 4, 2007 Share Posted January 4, 2007 Something like this:[code]<?php$result = mysql_query("SELECT name FROM products");$i=0;while($r=mysql_fetch_array($result);{ if($i%2 == 0) echo "<font color=\"red\">".$r['name']."</font>"; else echo "<font color=\"blue\">".$r['name']."</font>"; $i++;}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/32833-multiple-row-colors/#findComment-152850 Share on other sites More sharing options...
Picabrillo Posted January 4, 2007 Share Posted January 4, 2007 You could try:[code]<?php$result = mysql_query("SELECT name FROM products");$bg='#eeeeee'; // First row background colorwhile($r=mysql_fetch_array($result);{ $bg = ($bg == '#eeeeee') ? '#ffffff' : '#eeeeee'; // Shorthand if statement (if $bg equals #eeeeee, add #ffffff to $bg, else add #eeeeee)?><tr style="background-color:<?php echo $bg; ?>"> Data for the row...</tr><?php}?>[/code]That'll alternate your background colours between 2 different colours, but not more than that. One way of doing more than 2 could be to store the colours in an array and access them that way. Like this:[code]<?php$result = mysql_query("SELECT name FROM products");$bg = array('#dasdas', '#bbbbbb', '#cccccc', '#dddddd', '#eeeeee', '#ffffff'); // Range of background colours in order to display$index = 0; // Which color in array to start at (0 being first one)while($r=mysql_fetch_array($result);{ $index = ($index < count($bg)) ? $index + 1 : 0; ?><tr style="background-color:<?php echo $bg[$index]; ?>"> Data for the row...</tr><?php}?>[/code]Both of these should work. Sorry if it's not very clear, let me know if it not working. Link to comment https://forums.phpfreaks.com/topic/32833-multiple-row-colors/#findComment-152860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.