Jump to content

multiple row colors


feri_soft

Recommended Posts

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

You could try:

[code]

<?php
$result = mysql_query("SELECT name FROM products");

$bg='#eeeeee';  // First row background color
while($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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.