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>

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 @broj1 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.