2

I have that

<?php
   $new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
   $dem=1;
   while($row_new=mysql_fetch_assoc($new)){
?>  
<div class=""row>
   <div class="col-3">
   </div>
</div>
<?php } ?>

I want when this code run, it'll display 1 row with 4 col-3 until the end of the loop.

I want output like this

<div class=""row>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
</div>
<div class=""row>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
   <div class="col-3">
   </div>
</div>

Please help me! Thanks all!

Nam Nguyen
  • 23
  • 8

3 Answers3

0

try this bro:

update, if count rows less then 4:

$new = get_records("tbl_item", "status=1 AND idshop='{$idshop}' AND special", "id DESC", $startRow . "," . $pageSize, " ");
$dem = 1;
$x = 0;
$count = count($new);
while ($row_new = mysql_fetch_assoc($new)) {
    $x++;
    if ($x==1 || $x % 4 == 1) { ?>
        <div class="row">
    <?php } ?>
    <div class="col-3">
        <?= "x: $x" ?>
    </div>
    <?php if ($x % 4 == 0 || $x == $count) { ?>
        </div>
        <?php
    }
}
Jacky Supit
  • 469
  • 3
  • 15
0

This should work:

<?php
    $new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
    $i=0;
    while($row_new=mysql_fetch_assoc($new)){
        if($i % 4 == 0) {
?>  
<div class="row">
<?php
        }
?>  
    <div class="col-3">
    </div>
<?php
        if($i % 4 == 3) {
?>  
</div>
<?php
        }
?>  
<?php 
        $i++;
    }
    $i--;
    if ($i % 4 != 3) {
?>  
</div>
<?php
    }
?>

The last if is in case the total number of records weren't a multiple of 4, then we need to close the div tag.

And, not to mention, you should definitely use MySQLi. Why should I use MySQLi?

Xpleria
  • 5,472
  • 5
  • 52
  • 66
-1

Try this:

<?php

   $new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
   $dem=1;

   while($row_new=mysql_fetch_assoc($new)){

?>  
<div class="row">

    <?php for($i = 1; $i <= 4; $i++){?>
       <div class="col-3">
       </div>
    <?php } ?>

</div>
<?php } ?>
Minsher
  • 11
  • 2