Solarpitch Posted November 25, 2006 Share Posted November 25, 2006 Hey guys,just wondering when you display data in a table, how do you display every second result with a different colour background? ie:result 1. greyresult 2. light greyresult 3. greyresult 4. light greyresult 5. grey.. etccheers! Quote Link to comment https://forums.phpfreaks.com/topic/28441-displaying-rows-of-data-with-an-alternating-colour-sequence/ Share on other sites More sharing options...
wildteen88 Posted November 25, 2006 Share Posted November 25, 2006 This can easily be accomplished using PHP in a while loop if you are getting data from a database with PHP. However if you are using plain old HTML then you have to code in manually or you can use javascript which will seek out your table rows and alternate the colors for each row one by one. Quote Link to comment https://forums.phpfreaks.com/topic/28441-displaying-rows-of-data-with-an-alternating-colour-sequence/#findComment-130165 Share on other sites More sharing options...
Solarpitch Posted November 25, 2006 Author Share Posted November 25, 2006 I am using PHP . . do you have any examples I can look at? Quote Link to comment https://forums.phpfreaks.com/topic/28441-displaying-rows-of-data-with-an-alternating-colour-sequence/#findComment-130173 Share on other sites More sharing options...
corbin Posted November 26, 2006 Share Posted November 26, 2006 Very very very basic example but here it goes:[code=php:0]<?//------------------------------Making some results------------------------------\\$result[] = "Hi, my name is corbin";$result[] = "This is row number 2";$result[] = "Guess what? Row 3!";$result[] = "Row4";$result[] = "5";$result[] = "6";$result[] = "7";$result[] = "8";$result[] = "9";$result[] = "10";//------------------------------End results------------------------------\\$color1 = "#808080"; //gray$color2 = "#CCCCCC"; //lightgray$rnum = count($result); //count $result$i = 0;echo "<table>"; //create the tablewhile($i < $rnum) { //start outputting the rows$color = $color1;if($i % 2) { $color = $color2; } /*if $i equally divides by 2 (if its even) then make the background color the second colorthe weird thing about modulus (the % operator) is that it returns true if there is a modulus (remainder)... so if it returns true $i is odd. */echo "<tr>";echo "<td bgcolor=\"{$color}\">"; //start a cellecho $result[$i]; //echo the resultecho "</td>"; //close the cell$i++;}echo "</table>"; //close the table...?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28441-displaying-rows-of-data-with-an-alternating-colour-sequence/#findComment-130401 Share on other sites More sharing options...
Solarpitch Posted November 26, 2006 Author Share Posted November 26, 2006 Thanks for that! Thats all I was looking for really . . now, ill go try and implement! :) Quote Link to comment https://forums.phpfreaks.com/topic/28441-displaying-rows-of-data-with-an-alternating-colour-sequence/#findComment-130437 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.