jesusislord25 Posted December 10, 2010 Share Posted December 10, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/ Share on other sites More sharing options...
Yucky Posted December 11, 2010 Share Posted December 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145662 Share on other sites More sharing options...
vodos Posted December 11, 2010 Share Posted December 11, 2010 <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> Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145681 Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2010 Share Posted December 11, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145776 Share on other sites More sharing options...
jesusislord25 Posted December 11, 2010 Author Share Posted December 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145806 Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2010 Share Posted December 11, 2010 Did you edit it so that ['trigger_field'] reflects the field you want to trigger the color change? I tested that script before I posted it, and it works as intended as long as it's configured right. Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145808 Share on other sites More sharing options...
jesusislord25 Posted December 11, 2010 Author Share Posted December 11, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1145815 Share on other sites More sharing options...
Yucky Posted December 13, 2010 Share Posted December 13, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1146479 Share on other sites More sharing options...
Pikachu2000 Posted December 13, 2010 Share Posted December 13, 2010 Using anything other than full <?php tag syntax undermines portability. Quote Link to comment https://forums.phpfreaks.com/topic/221247-group-alternating-background-color-based-on-category/#findComment-1146608 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.