Skip to content

Instantly share code, notes, and snippets.

@johnmorris
Created October 14, 2015 20:39
Show Gist options
  • Save johnmorris/e812fd0e7a2644443588 to your computer and use it in GitHub Desktop.
Save johnmorris/e812fd0e7a2644443588 to your computer and use it in GitHub Desktop.
Zebra striped table in a loop with odd even classes using PHP
<?php
function oddeven($count, $increment = 2) {
$state = $count%$increment;
if ( ! $state ) {
return 'even';
}
return 'odd';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Apply Odd/Even Classes In a Loop Using PHP</title>
<style>
table {
width: 500px;
}
td {
padding: 5px;
}
.even {
background: #ccc;
}
</style>
</head>
<body>
<table>
<?php for($i=1; $i<=20; $i++) : ?>
<tr class="<?php echo oddeven($i); ?>">
<td>Row <?php echo $i; ?></td>
</tr>
<?php endfor; ?>
</table>
</body>
</html>
@26kito
Copy link

26kito commented Jan 7, 2022

Woah thank you very much sir, even you posted this 6 yeas ago. But the code does still work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment