0

I cant seem to figure out how to implement any examples I found online of how to us a counter so that ever 3rd echo of " $row['item']" has a div in between it.

$result = mysql_query("SELECT * FROM table")
while($row = mysql_fetch_array( $result )) {
    echo  $row['item'] ;
}
0

2 Answers 2

1

When you want to do something every x loops, I find the easiest method is to use a modulo/modulus operator:

for($i=0;$i<20;$i++)
{
    if($i%3==0)
    {
        echo "This is the third time round...";
    }
}

you can easily implement this into a loop while fetching rows from the database.

$result = mysql_query("SELECT * FROM table")
$i=0;
while($row = mysql_fetch_array( $result )) 
{
    echo  $row['item'] ;
    if($i%3==0)
    {
        echo "Do your DIV stuff here...";
    }
    $i++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user2055697 I added an example into the answer of how to do it in a while loop.
1

Try

$result = mysql_query("select * from table")
$i=0;
while($row = mysql_fetch_array( $result )) 
{
    echo  $row['item'] ;
    if($i%3==0)
    {
        echo "<div>Your div content</div>";
    }
    $i++;
}

I would also suggest to go through for/while/foreach loop as well

2 Comments

If this isn't a carbon copy of Fluffeh's then I don't know what is. This is what we call "dirty pool", where I come from.
@Fluffeh LOL! It's nice to see that you've a sense of humour :)) well you do have a point there ;-) "close but no cigar"

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.