在PHP中获取行

时间:2017-10-20 08:01:06

标签: php

我有那个

<?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 } ?>

我希望当这段代码运行时,它会在循环结束前显示1行4 col-3。

我想要这样的输出

<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>

请帮帮我!谢谢大家!

3 个答案:

答案 0 :(得分:0)

试试这个兄弟:

更新,如果计数行少于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
    }
}

答案 1 :(得分:0)

试试这个:

<?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 } ?>

答案 2 :(得分:0)

这应该有效:

<?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
    }
?>

最后if如果记录总数不是4的倍数,那么我们需要关闭div标记。

而且,更不用说,你绝对应该使用MySQLi。 Why should I use MySQLi?