A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearEasiest way to alternate row colors in PHP/HTML? - page 1
User InfoPosts
Easiest way to alternate row colors in PHP/HTML?#1
Here's a PHP example of mine. Can anyone find a shorter/easier way to do this?

<? foreach($posts as $post){?>
<div class="<?=($c++%2==1)?‘odd’:NULL?>">
<?=$post?>
</div>
<? }?>

<style>
.odd{background-color:red;}
</style>


Examples in other languages would be fun to see as well.

posted date: 2008-12-29 15:56:00


Re: Easiest way to alternate row colors in PHP/HTML?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-29 15:56:01


Re: Easiest way to alternate row colors in PHP/HTML?#3
It's short enough as it is, but I would probably wrap it into some helper function with a clear name. That way it's more obvious what's going on and you won't have to repeat that logic in all templates where you need it.

posted date: 2008-12-29 16:03:00


Re: Easiest way to alternate row colors in PHP/HTML?#4
You only define the function once, then have a nice short and very readable call to the function everywhere you want to use it.

posted date: 2008-12-29 16:05:00


Re: Easiest way to alternate row colors in PHP/HTML?#5
<?php $alt = true; foreach ($posts as $post): $alt = !$alt; ?><div<?php echo $alt ? ' class="odd"' : ''; ?>> <!-- Content --> </div> <?php endforeach ?>Would be the simplest and clearest way to do it.

posted date: 2008-12-29 16:06:00


Re: Easiest way to alternate row colors in PHP/HTML?#6
If you want to do it on the display end and are comfortable with or otherwise already using javascript, libraries like jQuery will often have :odd and :even selectors, which you can then hook up to adding specific style properties or hooking into CSS more generally by adding classes.

posted date: 2008-12-29 16:14:00


Re: Easiest way to alternate row colors in PHP/HTML?#7
That seems like a pretty good example but it seems like for these basics it's nice to have a snippet that you can just throw in whenever it pops up.

posted date: 2008-12-29 16:20:00


Re: Easiest way to alternate row colors in PHP/HTML?#8
If you'd like to have less in-line PHP, a great way of doing it is via JavaScript.Using jQuery, it's simply:<script type="text/javascript">$('div:odd').css('background-color', 'red');</script>

posted date: 2008-12-29 16:23:00


Re: Easiest way to alternate row colors in PHP/HTML?#9
pretty much the same as @Vilx- (who posted earlier) but a good one nonetheless.

posted date: 2008-12-29 16:24:00


Re: Easiest way to alternate row colors in PHP/HTML?#10
Just for fun Assuming you can use CSS3 selectors you can do something like<div class="posts"><? foreach($posts as $post){?> <div> <?=$post?> </div><? }?></div><style> div.posts div:odd{background-color:red;}</style>Even with CSS2 support and mootools (javascript library) you can substitute the style with this javascript <script type="text/javascript"> // obviously this script line should go in a js file in a onload (or onDomReady) function $$('div.posts div:odd').setStyle('background-color','red');</script>If you don't have anything but php a it you can simplify a bit yous code using an array<? $isodd=array('','odd'); $c=0; foreach($posts as $post){?> <div class="<?=$isodd[$c++%2]?>"> <?=$post?> </div><? }?>

posted date: 2008-12-29 16:40:00


Re: Easiest way to alternate row colors in PHP/HTML?#11
I don't use jQuery but that looks like a nice, clean bit of code to use.

posted date: 2008-12-29 16:43:00


Re: Easiest way to alternate row colors in PHP/HTML?#12
You can encapsulate the logic as follows:<?phpclass ListCycler { private $cols, $offs, $len; // expects two or more string parameters public function __construct() { $this->offs = -1; $this->len = func_num_args(); $this->cols = func_get_args(); foreach($this->cols as &$c) $c = trim(strval($c)); } // the object auto-increments every time it is read public function __toString() { $this->offs = ($this->offs+1) % $this->len; return $this->cols[ $this->offs ]; }}?><html><head> <style> ul#posts li.odd { background-color:red; } ul#posts li.even { background-color:white; } </style></head><body> <div> <h3>Posts:</h3> <ul id="posts"><?php $rc = new ListCycler('odd','even'); foreach($posts as $p) echo "<li class='$rc'>$p</li>"; ?></ul> </div></body></html>

posted date: 2008-12-29 22:40:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.