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
Share on other sites

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