foreach alternate like table |
[eluser]Mutsop[/eluser]
Hi, How can you alternate the classes of the divs within your view? Code: <?php foreach($list as $item):?> So instead of only class "a" it should be "a", "b", loop... Regards
[eluser]danmontgomery[/eluser]
You need to keep a counter, and check if that counter is even or odd. You can do this with modular division. Code: $count = 0; You can shorten this with the ternary operator: Code: $count = 0; You can also use the alternator() function in the string helper: http://ellislab.com/codeigniter/user-gui...elper.html Code: for ($i = 0; $i < 10; $i++)
[eluser]WanWizard[/eluser]
Are your array keys numeric or it an an associative array? For numeric, try Code: foreach( $list as $key => $item ) For associative, try Code: $i = 0;
[eluser]cideveloper[/eluser]
not sure what you are using this for but if its just to style alternating divs then you can use the nth child selector for css HTML Code: <div id="alternating"> CSS Code: #alternating>div{ The 2n means every second child of the parent selector gets these attributes. This type of selection can also be use with jquery. So your code would look like this Code: <div id="alternating">
[eluser]Eric Barnes[/eluser]
Or just use the alternator helper: http://ellislab.com/codeigniter/user-gui...elper.html Code: <?php foreach($list as $item):?>
[eluser]Nick_MyShuitings[/eluser]
Curse your speed Eric Barnes... but in all seriousness I was wondering why nobody had brought up the string helper... quite possibly my favorite helper in the whole stack, and the only one that I always autoload. EDIT: F-ing time zone changes... I'm a good 5 hours behind what I thought I was... sigh... |
Welcome Guest, Not a member yet? Register Sign In |