Welcome Guest, Not a member yet? Register   Sign In
foreach alternate like table
#1

[eluser]Mutsop[/eluser]
Hi,

How can you alternate the classes of the divs within your view?
Code:
<?php foreach($list as $item):?>
                            <div class="a">
                                code
                            </div>
                        &lt;?php endforeach;?&gt;

So instead of only class "a" it should be "a", "b", loop...

Regards
#2

[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;
foreach($list as $item) {
    if($count % 2) {
        $class = 'a';
    } else {
        $class = 'b';
    }

    echo '<div class="'.$class.'">code</div>';

    $count++;
}

You can shorten this with the ternary operator:

Code:
$count = 0;
foreach($list as $item) {
    echo '<div class="'.($count++%2?'a':'b').'">code</div>';
}

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++)
{
    echo alternator('string one', 'string two');
}
#3

[eluser]WanWizard[/eluser]
Are your array keys numeric or it an an associative array?

For numeric, try
Code:
foreach( $list as $key => $item )
{
    $class = $key % 2 ? "odd" : "even"
}

For associative, try
Code:
$i = 0;
foreach( $list as $item )
{
    $class = $i++ % 2 ? "odd" : "even"
}
#4

[eluser]Mutsop[/eluser]
Thanks for the fast reply guys... really helpfull Smile
#5

[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">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
</div>

CSS
Code:
#alternating>div{
    width: 50px;
    margin: 20px;
    padding: 20px;
    background-color: red;
}

#alternating>div:nth-child(2n) {
    background-color: blue;
}

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">
&lt;?php foreach($list as $item):?&gt;
<div>
code
</div>
&lt;?php endforeach;?&gt;
</div>
#6

[eluser]Eric Barnes[/eluser]
Or just use the alternator helper:
http://ellislab.com/codeigniter/user-gui...elper.html
Code:
&lt;?php foreach($list as $item):?&gt;
                            <div class="&lt;?php echo alternator('a', 'b');?&gt;">
                                code
                            </div>
                        &lt;?php endforeach;?&gt;
#7

[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...




Theme © iAndrew 2016 - Forum software by © MyBB