Jump to content

[SOLVED] alternate row colour depending on variable?


ale1981

Recommended Posts

I can not figure this out, what I want to do is alternate the row colour of a table depending on a variable when looping through results from a db query, example;

 

ORD123

ORD123

ORD124

 

and so on..

 

<table>
$trclass = 'alt1'
$trclass = 'alt2'
while ($ord_inf = mssql_fetch_array($result)) {
  if ($ordinf['ORDERNO'] == $orderNo) {
    'keep same tr class as its the same order'
  } else {
    'its a different order so change the tr class'
  }
    <tr class="$trclass">
      <td>$ord_inf['ORDERNO']</td>
    </tr>
  $orderNo = $ord_inf['ORDERNO'];
}
</table>

 

something like that, i just cant seem to get it to work!

 

any help appreciated...

 

[/]

try

<table>
<?php
$trclass_array = array('alt1', 'alt2');
$orderNo = '';
$color = 1;
while ($ord_inf = mssql_fetch_array($result)) {
  if ($ordinf['ORDERNO'] != $orderNo) {
  	$color = 1 - $color;
  	$orderNo = $ordinf['ORDERNO'];
  }
  $trclass = $trclass_array[$color];
  //... etc
}
?>
</table>

SOLUTION for anybody interested;

 

<table>
<?php
while ($ord_inf = mssql_fetch_array($result))
{
        if($lastOrderNo != $ord_inf['ORDERNO'] || $lastOrderNo == "")
        {
                $trclass = ($trclass == 'alt2') ? 'alt1' : 'alt2';
        }
        $lastOrderNo = $ord_inf['ORDERNO'];
        ?>
        <tr class="<?php echo $trclass; ?>">
          <td><?php echo $ord_inf['ORDERNO']; ?></td>
        </tr>
        <?php
}
?>
</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.