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
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; ?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

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.