Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coloured Date Rows 2

Status
Not open for further replies.

pastorandy

IS-IT--Management
Nov 2, 2006
84
GB
Hi

I have a query that returns rows of records from a mysql database and I was wondering how I would alternate the cell colours for the rows that are of the same day.

I could have 1 or more records returned for each day.

Code:
Jan 1  This is the first record
Jan 1  This could be the second record
Jan 2  This might be the third record

So I would need say, all Jan 1st records to have one cell background colour and all Jan 2nd records to have a different colour and so on...

anybody have any ideas?
 
Hi

CSS:
tr.row0 { background-color: #ccf; }
tr.row1 { background-color: #cfc; }
Code:
$query="select the_date,the_rest from the_table order by the_date";
$result=mysql_query($query,$connection);

echo "<table>\n<tr><th>Date</td><th>Rest</td></tr>\n";

$last_date='';
$style=0;
while ($row=mysql_fetch_assoc($result)) {
  if ($last_date!=$row[the_date]) {
    $last_date!=$row[the_date];
    $style=1-$style;
  }
  echo "<tr class=\"row$style\"><td>$row[the_date]</td><td>$row[the_rest]</td></tr>\n";
}

echo "</table>\n";

Feherke.
 
I think these lines:

Code:
if ($last_date!=$row[the_date]) {
  $last_date!=$row[the_date];
  $style=1-$style;
}

should actually be:

Code:
if ($last_date!=$row[the_date]) {
  [b]$last_date=$row[the_date];[/b]
  $style=1-$style;
}
 
No problem. If they started taking away points for little mistakes in code like that one, we'd all end up deep in the negatives within a few days.

I've filed away that trick with "$style" and the numbers, by the way. All the ways I'd done that before are a lot less elegant.
 

I this bit right?

Code:
<tr class=\"row$style\">
 
Yes. PHP will interpret the variable and replace it with it's value during execution. So if $style == 0, then \"row$style\" will be output as "row0".
 
Does it matter that I am outputting my date like this
Code:
echo date('d-m-Y',strtotime($row[job_date]));

At the moment it doesn't change any colours for me..
 
The format of your date shouldn't matter. Have you added the CSS definitions into the style section of your html head? It should look something like:

HTML:
<head>
 <title>...</title>
 <style type="text/css">
  tr.row0 { background-color: #ccf; }
  tr.row1 { background-color: #cfc; }
 </style>
...
</head>
...
 
Yep, I had them in but just noticed that my TD Class was overwritting them.

Many thanks Guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top