davidcriniti Posted August 10, 2010 Share Posted August 10, 2010 ...I'm Australian, hence the 'u' in coloured, in case you're wondering! Basically I'd like to create a table of results for a running race that has rows shaded pink if the participant is female. I think my problem is something to do with not declaring 'sex' as a variable, but I'm not really sure how to do this. My code is below. Any advice would be much appreciated. <?php $dbcnx = @mysql_connect('localhost', 'MYUSERNAME', 'MYPASSWORD); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('coast2ko_test')) { exit('<p>Unable to locate the results ' . 'database at this time.</p>'); } $sql = mysql_query("SELECT * FROM result_single ORDER BY place ASC"); echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>"; echo "<tr> <td><H3>First name</h3></td> <td><H3>Lastname</H3></td> <td><H3>Sex</H3></td> <td><H3>Time</H3></td><td><H3>Place</H3></td></tr>"; // keeps getting the next row until there are no more to get while ($row = mysql_fetch_array($sql)) { // Print out the contents of each row into a table if ($sex=="F") echo "<tr bgcolor='#FF99FF'>"; else echo "<tr>"; echo "<tr bgcolor='#FF99FF'>"; echo "<td>"; echo $row['firstname']; echo "</td><td>"; echo $row['lastname']; echo "</td><td>"; echo $row['sex']; echo "</td><td>"; echo $row['time']; echo "</td><td>"; echo $row['place']; echo "</td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/ Share on other sites More sharing options...
JasonLewis Posted August 10, 2010 Share Posted August 10, 2010 Look at your code above. There's an error.. and it's right at the top. Fix that, then use $row['sex'] instead of $sex. Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1097433 Share on other sites More sharing options...
davidcriniti Posted August 10, 2010 Author Share Posted August 10, 2010 Thanks ProjectFear, I got it working. I notice I'd also thrown in another " echo "<tr bgcolor='#FF99FF'>"; " after the if / else statement which made things a bit more difficult, but I got there in the end. Just out of interest, was the error you were referring to at the top of my code the missing " ' " after the word PASSWORD, or is there something else? Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1097523 Share on other sites More sharing options...
JasonLewis Posted August 10, 2010 Share Posted August 10, 2010 Yeah, it was. But I guess you could have accidentally deleted that single quote when removing your password. What was wrong with your script? Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1097524 Share on other sites More sharing options...
davidcriniti Posted August 10, 2010 Author Share Posted August 10, 2010 There's nothing wrong with it anymore I'm pleased to say! Thanks again for your help. All in all it was 3 things: - The " ' " after PASSWORD - and yep, that was just inadvertently deleted as I deleted the password. - Using $sex until you advised me to change it to $row['sex']. - Under the if / else statement, I"d also had (for some reason unbeknownst to myself!) " echo "<tr bgcolor='#FF99FF'>"; " which I didn't notice until a bit later. That caused all cells to have a pink background, regardless of the sex of the athlete. Goes to show you can't take you're eye off the ball for a second! Thanks again, Dave Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1097775 Share on other sites More sharing options...
Psycho Posted August 10, 2010 Share Posted August 10, 2010 I would offer two slight modifications that would make the code a little more elegant. Each has different benefits 1. Use the ternary operator. This is straitforward and can be easily implemented in your existing code. Example while ($row = mysql_fetch_array($sql)) { // Print out the contents of each row into a table $bgColor = ($row['sex']=="F") ? '#FF99FF' : '#FFFFFF'; echo "<tr bgcolor=\"{$bgColor}\">\n"; echo "<td>{$row['firstname']}</td>\n"; echo "<td>{$row['lastname']}</td>\n"; echo "<td>{$row['sex']}</td>\n"; echo "<td>{$row['time']}</td>\n"; echo "<td>{$row['place']}</td>\n"; echo "</tr>\n"; } echo "</table>"; 2. Set the colors in an array. This allows you to set the colors at the top of the script, or even in a config file, so you can change the colors without modifying the logic code. Example: $bgColors = array('F'=>'#FF99FF', 'M'=>'#FFFFFF'); while ($row = mysql_fetch_array($sql)) { // Print out the contents of each row into a table echo "<tr bgcolor=\"{$bgColors[$row['sex']]}\">\n"; echo "<td>{$row['firstname']}</td>\n"; echo "<td>{$row['lastname']}</td>\n"; echo "<td>{$row['sex']}</td>\n"; echo "<td>{$row['time']}</td>\n"; echo "<td>{$row['place']}</td>\n"; echo "</tr>\n"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1097800 Share on other sites More sharing options...
davidcriniti Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks for your time mjdamato, That worked perfectly! Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/210309-different-coloured-rows/#findComment-1098047 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.