-3

My code is below:

function portfolio_gallery() {
    global $conn;   
    $query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");

    if ($query->num_rows > 0) {
        // output data of each row
        echo '<div>';
        $i = 0; 
        while($row = $query->fetch_assoc()) {
            $i++;
            if ($row["showimage"]) {

               if($i % 9 == 0){
                     echo '</div><div>';
            }

              echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
              }

        }
        echo '</div>';
    }
}

portfolio_gallery();

I wanted to echo </div><div> for every after 9th item of the loop but every time I executed the code, the first echo only happened after 8 items instead of 9, but the rest was every 9th.

hello
  • 351
  • 3
  • 4
  • 16

5 Answers5

0

You have to increment

$i

after

if($i % 9 == 0)
Zimm1
  • 433
  • 5
  • 16
0

declare $i = 1 and Write $i++ at the end of while loop.

if ($query->num_rows > 0) {
    // output data of each row
    echo '<div>';
    $i = 1; // declare $i = 1 here
    while($row = $query->fetch_assoc()) {

        if ($row["showimage"]) {
           if($i % 9 == 1){ // 10 , 19 ,28
                 echo '</div><div>';
           }
           echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
        }
        $i++; // increment at end of the loop
    }
    echo '</div>';
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • I did this and it counted every 9th, except for the first which echoed after no items, like so:`
    `
    – hello Jul 04 '16 at 10:34
0

follow the syntax example i worked out its working

    <?php
    $j=0; 
    for($i=0;$i<=50;$i++)
    {
        if($j==9)
        {

            echo $j.'hioiiiiiii<br/>';  //echo "</div><div>";
         $j=-1; 
        }
       $j++;
    }

    ?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

Please try this :)

<?php 
 function portfolio_gallery() {
    global $conn;   
    $query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");

if ($query->num_rows > 0) {
    // output data of each row
    echo '<div>';
    $i = 0;
    while($row = $query->fetch_assoc()) {
        if ($row["showimage"]) {

           if($i % 9 == 0){
                echo '</div><div>';
            }

            echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
        }
    $i++;
    }
    echo '</div>';
}

}

Kuldeep
  • 202
  • 3
  • 8
0

I was able to solve it by modifying the condition:

So instead of: if($i % 9 == 0) {...}

I used: if($i!=0 && $i % 9 == 0) {...}

And also placing $i++ at the end of while loop.

hello
  • 351
  • 3
  • 4
  • 16