Created
October 14, 2015 20:39
-
-
Save johnmorris/e812fd0e7a2644443588 to your computer and use it in GitHub Desktop.
Zebra striped table in a loop with odd even classes using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Woah thank you very much sir, even you posted this 6 yeas ago. But the code does still work for me