2

I would like to make this in a loop:

<div class="global">
    <div class="left">1</div>
    <div class="right">2</div>
</div>
<div class="global">
    <div class="left">3</div>
    <div class="right">4</div>
</div>
<div class="global">
    <div class="left">5</div>
    <div class="right">6</div>
</div>
<div class="global">
    <div class="left">7</div>
    <div class="right">8</div>
</div>
<div class="global">
    <div class="left">9</div>
    <div class="right">10</div>
</div>

I know do something link this :

for($i=0;$i<4;$i++){
    if($i %2){
        $classe='class="right"';
    }
    else{   
        $classe='class="left"';
    }

    echo "<div ".$classe." >".$i."</div>";
}

which result:

<div class="left">1</div>
<div class="right">2</div>
<div class="left">3</div>
<div class="right">4</div>

How Can I integrate the div "global" between ?

Thanks a lot for your help

6
  • You are italian, aren't you? "Classe" and "Modulo" are 2 italian words. I'd just suggest you to learn to write your code and comments in english so you will not bother to translate them later. Commented Feb 3, 2011 at 18:06
  • @Charlie Pigarelli: actually "modulo" is English too! ;) Commented Feb 3, 2011 at 19:37
  • Didn't knew that. "Classe" is still something foreign for you, so my thoughts could be still right. Commented Feb 4, 2011 at 14:25
  • Updated Version II so that it works properly :) Commented Feb 7, 2011 at 20:08
  • Added Version II.b in response to your extended question. Commented Feb 17, 2011 at 6:06

4 Answers 4

2

Iterate two by two:

for ($i = 1; $i <= 4; $i += 2) {
    echo '<div class="global">';
    echo '<div class="left">' . $i . '</div>'
    echo '<div class="right">' . ($i+1) . '</div>'
    echo '</div>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Or simply use the OP approach and add the echo '<div class="global">' statement at the beginning of the loop and echo '</div>' at the end.
@nico, this is not what the user wants. (Look the first code)
@Charlie Pigarelli: How so? It is what he wants, see for instance @Surreal Dreams answer.
According to your answer we will have only one div with class="global", while in the output he wants to get there should be a div with class="global" for each row of the loop.
No, we will have one div class="global" per each left,right pair. This is what OP wants.
1

You just need a little extra...

echo '<div class="global">';  //start the first global div

for($i=0;$i<4;$i++){
    if($i %2){
        $classe='class="right"';
    }
    else{   
        $classe='class="left"';
    }

    echo "<div ".$classe." >".$i."</div>";
    if($i %2)
    {
        //after each "right" div, close and open a new global div
        echo "</div>\n<div class=\"global\">";
    }
}

echo '</div>';  //close the final global div

You can also compact the whole thing a bit:

echo '<div class="global">';  //start the first div

for($i=0;$i<4;$i++)
{
    if($i %2)
    {
        echo "<div class=\"right\" >$i</div>\n</div>\n<div class=\"global\">";
    }
    else
    {
        echo "<div class=\"right\" >$i</div>";
    }
}

echo '</div>';  //close the final global div

1 Comment

Nope. The original question never suggested it needed to.
1

I like printf.

$i=1;
while ($i < 8) {
    printf('<div class="global"><div class="left">%d</div><div class="right">%d</div></div>', $i++, $i++);
}

Edit: Although this doesn't really answer the OP question, and none of the other answers (yet) use modulus as per the question title. So, here is another far more ugly way :)

echo '<div class="global">';

for($i=0;$i<8;$i++){
    if ($i %2) {
        $classe='right';
        $sep='</div><div class="global">';
    }
    else{   
        $classe='left';
        $sep='';
    }

    printf('<div class="%s">%d</div>%s', $classe, $i+1, $i<7?$sep:'');
}

echo '</div>';

2 Comments

I love the echo way. Wanna start a echo-vs-printf fight?
I use both and I have no idea what determines when I switch! That is no good lol.
0

This code works :

echo '<div class="global">';  //start the first global div

for($i=0;$i<7;$i++){
    if($i %2){
        $classe='class="right"';
    }
    else{   
        $classe='class="left"';
    }

    echo "<div ".$classe." >".$i."</div>";
    if($i %2)
    {
        //after each "right" div, close and open a new global div
        echo "</div>\n<div class=\"global\">";
    }
}

echo '</div>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.