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

 

[/]

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

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.