Jump to content

Different coloured rows


davidcriniti

Recommended Posts

...I'm Australian, hence the 'u' in coloured, in case you're wondering!  :D

 

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>";



?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.