thecore7
thecore7

Reputation: 484

Something wrong with this for loop

I have this loop which doesn't work correct and I will be very thanksful if you can show me where is the problem.. Here is:

$num_rows = 3;
echo '<div id="slideshow"><div>';
for ($i = 0; $i < $num_rows; $i++) {
    echo'<div class="">text</div>';
    if (($i + 1) % 1 == 0)
        echo '</div><div>';
}
echo '</div></div>';

The problem is that this draws one more empty div.. The result is:

<div><div class="">text</div></div>    
<div><div class="">text</div></div>    
<div><div class="">text</div></div>    
<div></div>    

Why is this? thank for your help!

Upvotes: 0

Views: 74

Answers (2)

Tabish
Tabish

Reputation: 1722

$num_rows = 3;
echo '<div id="slideshow"><div>';
for ($i = 0; $i < $num_rows; $i++) {
    echo'<div class="">text</div>';
    if (($i + 1) % 1 == 0)
        echo '</div>';
}
echo '</div>;

Upvotes: 0

Syed Shoaib Abidi
Syed Shoaib Abidi

Reputation: 2366

The problem in your code is as below:

$num_rows=3;    
echo '<div id="slideshow"><div>';
for($i=0;$i<$num_rows;$i++)
{
    echo'<div class="">text</div>';
    if(($i+1)%1==0) echo '</div><div>';    
}                                 ^this is the start of empty div
echo '</div></div>';    
              ^this is the end of empty div

After removing them your code should be like below:

$num_rows=3;    
echo '<div id="slideshow"><div>';
for($i=0;$i<$num_rows;$i++)
{
    echo'<div class="">text</div>';
    if(($i+1)%1==0) echo '</div>';    
}    
echo '</div>';    

As you were creating an empty div yourself. Hope this helps.

Upvotes: 1

Related Questions