Forum Moderators: coopster

Message Too Old, No Replies

Alternate Row Colors

         

LinusIT

8:56 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I'm trying to alternate the background color of a table row but having great difficultly. I've searched and read all sorts but there seems to be so many different methods of doing it.

Here's all the relevant code:

<table cellpadding="0" cellspacing="0" id="models">
<tr class="headings">
<th class="model">Model</th>
<th class="weight">Weight</th>
<th class="price">Delivered</th>
<th class="price">10 Miles</th>
<th class="price">15 Miles</th>
<th class="price">20 Miles</th>
</tr>
<?php
$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;
?>
<? while($row=mysql_fetch_array($result)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr style="background-color:<? $row_color ?>;">
<td><?=$row['model_name']?></td>
<td><?=$row['model_weight']?>kg</td>
<?php
$subtotal = sprintf("%.2f", ($row['model_weight'] / 1000) * $price);
$total = floor($subtotal);
if ($total > 0) {
$total10 = $total - 30;
$total15 = $total - 40;
$total20 = $total - 50;
}
else
{
$total10 = $total;
$total15 = $total;
$total20 = $total;
}
?>
<td>&pound;<? echo $total ?></td>
<td>&pound;<? echo $total10 ?></td>
<td>&pound;<? echo $total15 ?></td>
<td>&pound;<? echo $total20 ?></td>

</tr>
<?
$row_count++;
} ?>
</table>


I'm hoping someone can help, the results are hard to read at present and this should make things easier.

Thanks

LinusIT

9:11 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I've sussed it, just need to echo .$row_color.

<tr style="background-color:<? echo $row_color ?>;">

Matthew1980

9:35 pm on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've sussed it, just need to echo .$row_color.

<tr style="background-color:<?php echo $row_color; ?>;">

Ok, great, just make sure that you have the correct declarations on the opening tags, as this could/can/will/does kill scripts if short_tags aren't enabled in the ini file, better to be safe than sorry.

Cheers,
MRb

LinusIT

9:44 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I wish I knew what you were talking about Matthew1980, I'm learning php as I go along, no books or anything. I'm getting there though :)

Thanks

Matthew1980

10:10 pm on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, my mistake, I will clarify..

Example:-

Good practise, works on all server configs:

<?php echo "This is a good piece of code"; ?>

Bad practise, only works on some servers where short tags are specified.

<? echo "This is a bad piece of code"; ?>

Because of the missing 'php' your not declaring the document properly, though this is acceptable, it is not preferred.

Hope that makes sense now.

Cheers,
MRb

LinusIT

12:46 pm on Feb 17, 2011 (gmt 0)

10+ Year Member



That does make sense and I do recall reading that somewhere.

I'll make sure I use <?php ?> in future.

Thanks

rocknbil

7:37 pm on Feb 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The way I do it, no modulus or count necessary, moves towards sepration of content and style:

<style type="text/css">
.first_tint { background: #CCFFCC; }
.second_tint { background: #BFD8BC; }
</style>

A good habit is to apply your colors to the cells, not rows, what if you have a column on the far right for subtotal that's a different color? :-)

$color1 = 'first_tint';
$color2 = 'second_tint';
$out=null;
//
while($row=mysql_fetch_array($result)) {
$row_color = ($row_color==$color1)? $color2:$color1;
//
$out .= "
<tr>
<td class=\"$row_color\">" . $row['model_name'] . "</td>
<td class=\"$row_color\">" . $row['model_weight'] . "kg</td> </tr>
";
}

if ($out) { echo "<table>$out </table>"; }
else { echo "<p>No records found.</p>"; }

coopster

10:22 pm on Feb 17, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A good habit is to apply your colors to the cells, not rows, what if you have a column on the far right for subtotal that's a different color? :-)


Don't forget though that you'll have less markup (lighter html) if you put your class on the table row. If the last column, or any column for that matter, needs a different color you can use a different class on that particular <td> instead and the specificity [w3.org] will apply.

$row_color = ($row_color==$color1)? $color2:$color1;


You can indeed use an inverse but you will need to declare the $row_color variable prior to using it in a ternary operation otherwise you'll get ye ol' "Notice: Undefined variable:" message thrown at you. Initialize it to the opposite value that you want your first row to start with.

coopster

3:13 pm on Feb 28, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



example

<style> 
.ri {background-color:green;}
.triple {color:red;}
.six {color:blue;}
</style>
<?php
$l = '';
$ri = 0;
foreach (range(1,9) as $v) {
$o = str_repeat($v, 20); // pad it up to expand our cell a bit ;-)
$class = array();
$class[] = $ri++ & 1 ? '' : 'ri';
// more code here, maybe adding more classes; example:
if (!($v % 3)) {
$class[] = 'triple';
}
$classTD = $v == 6 ? ' class="six"' : ''; // special cell coloring
$class = array_unique(array_filter($class));
$class = $class ? ' class="' . implode(' ', $class) . '"' : '';
$l .= " <tr{$class}><td>Value: </td><td{$classTD}>{$o}</td></tr>\n";
}
print "<table>\n{$l}</table>";
exit;
?>