Jump to content

Alternating row colors


contiw

Recommended Posts

The intention is to alternate table rows background color between classes  "row0" and "row1" defined elsewhere.

Is this code valid ?

 

<?php $rowclass = 0; ?>

<table ... >

    <!-- BEGIN poll_option -->

    <tr class="row{ROWCLASS}"> ............ tried also : class="row<?= $rowclass ?>"

    <td>

..........

</td>

    </tr>

<?php $rowclass = 1 - $rowclass; ?>

<!-- END poll_option -->

</table>

 

In the source I cannot see any value for "$rowclass".

 

Thanks for helping.

Link to comment
https://forums.phpfreaks.com/topic/210039-alternating-row-colors/
Share on other sites

<style type="text/css">
.class1 { background: #CC0000; }
.class2 { background: #FF0000; }
</style>

<?php $i = 0; while ($i < 31) : ?> 
<table>
<!-- BEGIN poll_option -->
<tr class="<?php if ($i & 1) : echo "class2"; else : echo "class1"; endif; ?>">
	<td>
	..........
	</td>
</tr>
<!-- END poll_option -->
</table>
<?php $i++; endwhile; ?>

Multiple of different ways.

<style type="text/css">
.class1 { background: #CC0000; }
.class2 { background: #FF0000; }
</style>

<?php $i = 0; while ($i < 31) : ?> 
<table>
<!-- BEGIN poll_option -->
<tr class="<?php  echo ((++$i % 2) == 0) ? "class2" : "class1";?>">
<td>
..........
</td>
</tr>
<!-- END poll_option -->
</table>

shouldnt it be with the table tags outside of the loop? or your making lots of tables and not rows


<style type="text/css">
.class1 { background: #CC0000; }
.class2 { background: #FF0000; }
</style>

<table>
<?php $i = 0; $row = 0; while ($i < 31) 
{ 
?> 

<!-- BEGIN poll_option -->
<tr class="<?php $row = $row == 0 ? 1 : 0; ?>">
	<td>
	..........
	</td>
</tr>
<!-- END poll_option -->

<?php 
$i++;
} 
?>
</table>

Yeah, that would make the most sense, although I simply used the exact example provided.

 

Yes you can get rid of the extra if logic, however I definitely wouldn't reccomend adding to $i in the IF statement.

 

<style type="text/css">
.class1 { background: #CC0000; }
.class2 { background: #FF0000; }
</style>

<table>
<?php $i = 0; while ($i < 31) : ?> 
<!-- BEGIN poll_option -->
<tr class="<?php echo ($i & 1) ? "class2" : "class1"; ?>">
	<td>
	..........
	</td>
</tr>
<!-- END poll_option -->
<?php $i++; endwhile; ?>
</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.