rocky_88 Posted April 23, 2011 Share Posted April 23, 2011 I'm trying to set different colors on rows of a table using an array, Problem is I cannot get it to work right, i'm not sure where to place my for loop so that it works right and generates a different color for different row. $color = array(red,green,blue); while ($row = mysql_fetch_assoc($exec)) { echo '<tr>'; echo '<td>'.$row['brand_name'].'</td>'; echo '<td>'.$row['contact_name'].'</td>'; echo '<td>'.$row['contact_info'].'</td>'; echo '<td>'.$row['email'].'</td>'; echo '<td>'.$row['description'].'</td>'; echo '</tr>'; } I tried putting for loop only for tr, but it didnt work. for ($i=0;$i<=count($color);$i++) { echo "<tr bgcolor=\"".$color[$i]."\">"; } I cannot put it within the while loop, it'll repeat the rows for all the colors. So where exactly do I place the for loop so that it works right? Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/ Share on other sites More sharing options...
harristweed Posted April 23, 2011 Share Posted April 23, 2011 <?php $color = array(red,green,blue); $i=0; while ($row = mysql_fetch_assoc($exec)) { echo "<tr bgcolor=\"".$color[$i]."\">"; echo '<td>'.$row['brand_name'].'</td>'; echo '<td>'.$row['contact_name'].'</td>'; echo '<td>'.$row['contact_info'].'</td>'; echo '<td>'.$row['email'].'</td>'; echo '<td>'.$row['description'].'</td>'; echo '</tr>'; $i++; if($i==count($color))$i=0; } ?> Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205145 Share on other sites More sharing options...
rocky_88 Posted April 23, 2011 Author Share Posted April 23, 2011 @harristweed Thanks for the code, that worked. But, now after red, green & blue, its applying white color to a row, this happened when I used the for loop too, any idea how to get rid of that Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205146 Share on other sites More sharing options...
harristweed Posted April 23, 2011 Share Posted April 23, 2011 Could be a problem with array count not counting the values correctly, possible because the values in your array are not enclosed in quotes $color = array(red,green,blue); Should be: $color = array("red","green","blue"); Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205150 Share on other sites More sharing options...
rocky_88 Posted April 23, 2011 Author Share Posted April 23, 2011 Quote Should be: $color = array("red","green","blue"); Tried this, but didnt work. Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205152 Share on other sites More sharing options...
rocky_88 Posted April 23, 2011 Author Share Posted April 23, 2011 @harristweed Got it, i had forgotten to remove the for loop. Its working fine now, Thanks a lot for the assist. Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205154 Share on other sites More sharing options...
harristweed Posted April 23, 2011 Share Posted April 23, 2011 I've no idea. Find out whats goinjg on. Echo out $i. Echo count of array. Look at source of table. It must be something simple! Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205155 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2011 Share Posted April 23, 2011 And, if you want to use some css that is not obsolete - <?php // list of colors (must be valid css color specifiers - name, HEX, RGB, ...) $color = array('red','green','blue'); // build tr css color classes (tr.c0, tr.c1, ...) $styles = ''; foreach($color as $key => $value){ $styles .= "tr.c$key {background-color:$value;}\n"; } $max_color = count($color) - 1; // build table content $content = "<table>\n"; $i = 0; while ($row = mysql_fetch_assoc($exec)){ $content .= "<tr class='c$i'>"; $content .= '<td>'.$row['brand_name'].'</td>'; $content .= '<td>'.$row['contact_name'].'</td>'; $content .= '<td>'.$row['contact_info'].'</td>'; $content .= '<td>'.$row['email'].'</td>'; $content .= '<td>'.$row['description'].'</td>'; $content .= "</tr>\n"; $i++; if($i > $max_color){ $i = 0; } } $content .= "</table>\n"; ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Cycle between table row colors</title> <style type="text/css"> table {border-collapse:collapse;} <?php echo $styles; ?> </style> </head> <body> <?php echo $content; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/234494-generate-different-colors-on-table-rows-using-array/#findComment-1205188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.