Jump to content

Group, alternating background color based on category


jesusislord25

Recommended Posts

Good Day,

I am trying to create a table where the background color alternates between colors based on the category.  The query sorts the category in alphabetical order.  So for every category that is the same the background color will be the same.  When the category changes, the background color changes.  Alternating between two colors will be fine.  I am not even sure where to begin on the sort of loop.

 

So I want php to create a table like below:

 

<table width="300" border="1" cellspacing="0" cellpadding="0">

          <tr bgcolor="#CCCCCC">

            <td width="117">Category 1</td>

            <td width="177">Excetion data</td>

          </tr>

          <tr bgcolor="#CCCCCC">

            <td>Category 1</td>

            <td>Excetion data</td>

          </tr>

          <tr bgcolor="#CCCCCC">

            <td>Category 1</td>

            <td>Excetion data</td>

          </tr>

          <tr bgcolor="#FFFFCC">

            <td>Category 2</td>

            <td>Excetion data</td>

          </tr>

          <tr bgcolor="#FFFFCC">

            <td>Category 2</td>

            <td>Excetion data</td>

          </tr>

          <tr>

            <td bgcolor="#CCCCCC">Category 3</td>

            <td bgcolor="#CCCCCC">Excetion data</td>

          </tr>

          <tr>

            <td bgcolor="#CCCCCC">Category 3</td>

            <td bgcolor="#CCCCCC">Excetion data</td>

          </tr>

          <tr>

            <td bgcolor="#CCCCCC">Categor 3</td>

            <td bgcolor="#CCCCCC">Excetion data</td>

          </tr>

        </table>

Link to comment
Share on other sites

A bit rough, but here goes:

<table width="300" border="1" cellspacing="0" cellpadding="0">
<?php
foreach($yourArray as $key => $value):
switch($value['yourCategory']) {
case 1:$colour = "00000";break;
case 2:$colour = "FFFFFF";break;
}
?>
          <tr>
            <td bgcolor="#<?=$colour;?>"><?=$value['yourCategoryName']; ?></td>
            <td bgcolor="#<?=$colour;?>"><?=$value['yourCategoryData']; ?></td>
          </tr>
<?php endforeach; ?>
</table>

 

Wouldn't be too hard to move the logic into the function that returns your data array.

Link to comment
Share on other sites

<table width="300" border="1" cellspacing="0" cellpadding="0">
<?php
$yourArray=array('1','0','asd');
foreach($yourArray as $key => $value){
if (intval($value)==0){
    
    $colour="red";
}else if(intval($value)==1)
$colour="blue";
else
$colour="";
?>
          <tr>
            <td bgcolor="<?=$colour;?>"><?=$value; ?></td>
            <td bgcolor="<?=$colour;?>"><?=$value; ?></td>
          </tr>
<?}?>
</table>

Link to comment
Share on other sites

First off, don't use the <?= 'quick echo' syntax. It'll come back to haunt you. Just write it properly the first time.

 

As for the original problem, I'm assuming you have more than just 2 possible values you'd like to use to trigger a change of BG color.

 

Where $array['trigger_field'] is the name of the field in your table that will trigger the change of BG colors:

$i = 1;
while( $array = mysql_fetch_assoc($result) ) {
$i = $trigger != $array['trigger_field'] ? $i+1 : $i;
$trigger = $array['trigger_field'];
echo "<tr bgcolor=\"";
echo $i % 2 == 0 ? 'CCCCCC' : '666666';  // BG color values
echo "\"><td>Your fields that are being echoed as results . . . </td><td>More result fields . . . </td></tr>";
}

Link to comment
Share on other sites

Pikachu2000:

I tried your code and it alternated colors between rows instead of between categories.  I have mutliple categories .  So there might be 5 rows with the same category, then say 3 rows with a different category, and then maybe 6 rows with yet another category.  This can go on and on.  What I want to do it make the rows the same color for each category.  So in the example above the first 5 rows would be color CCCCFF and then the next 3 rows would be color CCCCCC and then the next 6 rows would be back to CCCCFF.  So the background color would alternate color when the category changes.

 

I also played with the other posts, but I was not quite sure how to make it work with my query pulling in the results.

Link to comment
Share on other sites

Yes I did check that. 

 

However, I ran through the code again.  I had to change $i to $j since I was already using $i for something else.  I simply forgot to change one of the $i in your script to $j.  I changed that and it worked great.  Sorry for being a retard there.  I greatly appreciate your help and I love the the video you link to in your signature.

 

Have a Merry Christmas

Link to comment
Share on other sites

First off, don't use the <?= 'quick echo' syntax. It'll come back to haunt you. Just write it properly the first time.

 

I used quick tags for brevity in the example. Apart from that, there's not really anything wrong with it. It seems to be the cleanest and quickest way of doing what he wanted.

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.