Smee Posted June 6, 2010 Share Posted June 6, 2010 Hi, I posted an earlier article on the CSS forum section but have come to realise the problem is not CSS related and the solution was through PHP so i have given it a go and still seem to have no alternate colors. Can anyone see where i am going wrong with the following code: <?php require_once ('../mysql_connect.php'); echo ('<table width="294" height="150" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> </tr> </thead> '); $color1 = "#FFF"; $color2 = "#CCC"; $row_count = 0; $result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error()); while($row = mysql_fetch_assoc($result)) { $row_color = ($row_count % 2) ? $color1 : $color2; echo (' <tbody> <tr> <td bgcolor="$row_color">' . $row['first_name'] . '</td> <td bgcolor="$row_color">' . $row['post_code'] . '</td> <td bgcolor="$row_color">' . $row['team_supported'] . '</td> </tr> </tbody> '); $row_count++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/ Share on other sites More sharing options...
siva.katir Posted June 6, 2010 Share Posted June 6, 2010 I don't see where you are inserting the actual row color...? You assign the variables, but where do you actually insert them into the html? Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068672 Share on other sites More sharing options...
Smee Posted June 6, 2010 Author Share Posted June 6, 2010 Thanks for the help mate, What i thought should be happening is by using this code: $row_color = ($row_count % 2) ? $color1 : $color2; And having specified the colors in previous varials (color1 and color2) it will take what color is needed a placed in the bgcolor ="$row_color" section like below? <td bgcolor="$row_color">' . $row['first_name'] . '</td> I could be so far of the right method of doing this so my bad if its all a bit messed up heh Smee Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068676 Share on other sites More sharing options...
siva.katir Posted June 6, 2010 Share Posted June 6, 2010 Doh, I missed that in your code but see it now. Just curious what exactly is it putting out? It looks like that should work... Unless your PHP needs the variables more clearly written, ie: ".$color.". I know my PHP will sometimes just output $color instead of the contents of the variable. Your basic idea is right. I've always done it using css though with something like: echo ($i%2 ? 'odd' : 'even'); where $i is the row number. Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068682 Share on other sites More sharing options...
jd307 Posted June 6, 2010 Share Posted June 6, 2010 You seem to be on the right idea to produce what you are after, what I have done in the past (which may or may not be the best method, but it works) is something like the following: $color0 = '#FFF'; $color1 = '#CCC'; $row_count = 0; while($row = mysql_fetch_assoc($result)) { if ($row_count == 1) { $row_color = $color1; $row_count = 0; } else { $row_color = $color0; $row_count++; } echo ('<tbody> <tr bgcolor="$row_color"> <td>' . $row['first_name'] . '</td> <td>' . $row['post_code'] . '</td> <td>' . $row['team_supported'] . '</td> </tr> </tbody>'); } Basically this will just use color0 if the counter is "0" and increment the counter by 1. Otherwsie, it will use color1 and rest the counter. This way the counter will bounce between 0 and 1 all the way through your table alternating the colours which are associated to either the number 0 or the number 1. Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068687 Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 echo ($i%2 ? 'odd' : 'even'); Only keeps working if you never delete rows. if ($row_count == 1) { $row_color = $color1; $row_count = 0; } else { $row_color = $color0; $row_count++; } Is an unnecessary waste of resources. $color = 'even'; while ($row = mysql_fetch_assoc($result)) { echo '<td class="', ($color = ($color === 'even') ? 'odd' : 'even'), '">' .. Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068699 Share on other sites More sharing options...
Smee Posted June 6, 2010 Author Share Posted June 6, 2010 Thanks your method seems like it should work perfectly. I implemented it to my script and still do no get alternate colors, It still gives the right MySQL results just no color. It wouldn't be anything to do with CSS that needs adding as well, i mean i tried earlier with the CSS method and that didn't work either. Well anyway here is the new code thanks to JD307. <?php require_once ('../mysql_connect.php'); echo ('<table width="294" height="150" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> </tr> </thead> '); $result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error()); $color0 = '#FFF'; $color1 = '#CCC'; $row_count = 0; while($row = mysql_fetch_assoc($result)) { if ($row_count == 1) { $row_color = $color1; $row_count = 0; } else { $row_color = $color0; $row_count++; } echo ('<tbody> <tr bgcolor="$row_color"> <td>' . $row['first_name'] . '</td> <td>' . $row['post_code'] . '</td> <td>' . $row['team_supported'] . '</td> </tr> </tbody>'); } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068701 Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 That's because it's: echo ("<tbody> <tr bgcolor=\"$row_color\"> Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068703 Share on other sites More sharing options...
Smee Posted June 6, 2010 Author Share Posted June 6, 2010 Ok, Ignace i changed the section u mentioned but got the following error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ***/index.php on line 88 line 88 is: <td>' . $row['first_name'] . '</td> I then changed both " to ' like it was before but kept your \'s on $row_color and no errors but no alternate colors. There was me thinking this would be an easy job. <?php require_once ('../mysql_connect.php'); echo ('<table width="294" height="150" border="0" /> <thead> <tr> <th scope="col">First Name</th> <th scope="col">Post Code</th> <th scope="col">Team Supported</th> </tr> </thead> '); $color0 = "#FFF"; $color1 = "#CCC"; $row_count = 0; $result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error()); while($row = mysql_fetch_assoc($result)) { if ($row_count == 1) { $row_color = $color1; $row_count = 0; } else { $row_color = $color0; $row_count++; } echo ('<tbody> <tr bgcolor=\"$row_color\"> <td>' . $row['first_name'] . '</td> <td>' . $row['post_code'] . '</td> <td>' . $row['team_supported'] . '</td> </tr> </tbody>'); } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068717 Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 echo ("<tbody> <tr bgcolor=\"$row_color\"> <td>" . $row['first_name'] Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068720 Share on other sites More sharing options...
5kyy8lu3 Posted June 7, 2010 Share Posted June 7, 2010 I use modulus for this problem =) <?php $RowNumber = 0; foreach( $A as $B ) { if ( $RowNumber % 2 ) { $Style='red'; } else { $Style='blue'; } echo '<span class="' . $Style . '">' . $B . '</span>'; $RowNumber++; } Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068816 Share on other sites More sharing options...
TapeGun007 Posted June 7, 2010 Share Posted June 7, 2010 Here is an example of what I did on one of my webpages... very simple code. $counter++; if ($counter % 2) { $bgcolor="#ffffff"; } else { $bgcolor="#d9ece7"; } echo "<td bgcolor='".$bgcolor."'>Test</td>" It alternates every other row with white or light blue. Try it. Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068824 Share on other sites More sharing options...
kenrbnsn Posted June 7, 2010 Share Posted June 7, 2010 This <?php if ($counter % 2) { $bgcolor="#ffffff"; } else { $bgcolor="#d9ece7"; } ?> can be written <?php $bgcolor = ($counter % 2)?"#ffffff":"#d9ece7"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068826 Share on other sites More sharing options...
DavidAM Posted June 7, 2010 Share Posted June 7, 2010 What ignace is trying to tell you is that you cannot use variables inside a string that is delimited with single-quote marks: echo ('<tbody> <tr bgcolor="$row_color"> <td>' . $row['first_name'] . '</td> <td>' . $row['post_code'] . '</td> <td>' . $row['team_supported'] . '</td> </tr> </tbody>'); will put the literal string $row_color in your output echo ("<tbody> <tr bgcolor='$row_color'> <td>" . $row['first_name'] . "</td> <td>" . $row['post_code'] . "</td> <td>" . $row['team_supported'] . "</td> </tr> </tbody>"); will put the value of $row_color in your output Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068838 Share on other sites More sharing options...
Smee Posted June 7, 2010 Author Share Posted June 7, 2010 Thanks for all the info! It's all working fine, its good to see the different ways you could have done this. Smee Quote Link to comment https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/#findComment-1068994 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.