I have a gallery in grid.
For eg: https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSegNVXMSnTRzHETnAjZXCd6siwHQysMXc_jp1OyiSDie2_9jhsIg

Each row can cont max of 4 images.

I have written while loop.
i want to get the total count of images in each row

For eg: row1 -> 4 (images)
row2 -> 3 (images)

   <!----------------------------------------------------------------------------->    

   <div class="main_bg">
   <div class="top_bg1">
   <div class="">
          <div class="main">

       <div class="top_main">
              <h1>Today's Offer</h1>
              <div class="clear"></div>
            </div>



<?php
    $query1 = mysql_query("select * from offer");
    $x = 0;
    while($row = mysql_fetch_array($query1)){    
?>     

           <?php
           if($x==0){
           ?>
             <div class="grids_of_3">
           <?php
           }
           ?>



        <!-- start grids_of_3 -->

        <div class="grid1_of_3">
        <a href="offer_details.php?fid=<?php echo $row['offer_id']; ?>"> 
            <?php echo "<img src=admin/offer/".$row['image']." />"; ?>
            <h4><?php echo $row['offer_title']; ?></h4>
            <span class="price"><?php echo 'Rs: '.$row['price']; ?></span> <span class="price1 bg">Hot Sale</span> 
        </a> 
                <h3><a href="offer_details.php?fid=<?php echo $row['offer_id']; ?>" class="myButton">Add to cart</a></h3>

        </div>

         <?php
                $x++;
                echo $x;
                if($x==4){
                  echo '<div class="clear"></div></div>';
                  $x=0;

                }




   }  // while

?>

         <!-- start grids_of_3 -->

  </div>
  </div>
  </div>
  </div>
<!----------------------------------------------------------------------------->    


<!-- start footer -->

</div>
</div>

Dani AI

Generated

Two simple, reliable patterns will produce the per-row counts (max 4 items per row). As recommended, keeping per-row counts in an array is fine; an even more compact approach is to fetch the result set and split it into groups of four so each group’s size is the row count. (php.net)

Example (fetch-all then chunk — concise and easy to inspect):

$pdo = new PDO(/* dsn, user, pass */, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$stmt = $pdo->query('SELECT * FROM offer');
$offers = $stmt->fetchAll();              // all rows in an array

$rows = array_chunk($offers, 4);          // each element is a row-group
foreach ($rows as $index => $group) {
    $count = count($group);               // row count (4 or less)
    echo "row ".($index+1)." -> ".$count;
    // render $group items here
}

Note: fetchAll() returns the entire result set (easy to use, but memory-heavy for large tables). If the dataset can grow large, prefer streaming instead. (php.net)

Example (stream rows, keep a counter — low memory, simple HTML-wrapping logic):

$stmt = $pdo->query('SELECT * FROM offer');
$counter = 0;
$group = [];

while ($item = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $group[] = $item;
    $counter++;
    if ($counter === 4) {
        // render $group (count = 4)
        $group = [];
        $counter = 0;
    }
}
if ($counter > 0) {
    // render last partial row (count = $counter)
}

For modern PHP, migrate away from the old mysql_* functions — they were deprecated and later removed; use PDO or MySQLi instead. If streaming is needed with mysqli, take care to use the appropriate buffered/unbuffered mode. (php.net)

Practical tips: do not leave debugging echoes (the raw counter) inside the HTML, always close any open grid wrapper after the loop when the last group is partial, and use prepared statements to avoid injection when query parameters are involved. As observed, the array/counter ideas are equivalent; choose the one that best fits memory and code-style preferences. (wiki.php.net)

Recommended Answers

All 2 Replies

You can store number of images per row in an array (this is untested, just a concept):

// between lines 17 and 18 - initialize variables
$rowNo = 1;
$imagePerRow = array();

// between lines 44 and 45 - assign current value for that row
imagePerRow[$rowNo] = $x;

// on line 49 - increase row counter
$rowNo++;

To make it more efficient, store only a number for the last row, since all other rows have 4 images. The last row will have 4 or less images.

It seems like has got what you need, though I am not 100% sure on what it is you need done (I'm tired). If it's not what you want just reply and more examples and help will come.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.