每3条记录后新包含div

时间:2017-04-22 21:54:56

标签: php html if-statement while-loop

我想使用PDO结果循环在3个结果后创建一个包含<div>的新内容。

对于我的自学项目,我必须制作一个带有引导程序的产品页面,并且在每第3条记录之后我必须创建一个新行并再次显示3个col-md-4等等。

现在我将此作为我的代码:

<div class="row">
    <?php
    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
    ?>
        <div class="col-md-4">
            <div class="product">
                <div class="title"><?php echo $row->pname ?></div>
                <div class="img"><img
                    src="../product/img/<?php echo $row->pnumber ?>/<?php echo $row->pthumbnail ?>.jpg?$pop210x210$"/>
                </div>
                <div class="vijftien"></div>
                <div class="deliver">Levertijd: <strong><?php echo $row->pdelivertime ?></strong></div>
                <div class="vijf"></div>
                <div class="other"></div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="price"><?php echo $row->pprice ?></div>
                    </div>
                    <div class="col-md-6">
                        <div class="order">
                            <button class="log_in" id="doLogin">Meer informatie</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php } ?>
</div>

我已经访问并研究了其他问题,但我并不知道他们是如何做到的,以及如何在我的代码中实现正确的方法。

3 个答案:

答案 0 :(得分:4)

正如塔德曼在你提问的评论中所述。最佳方法应使用modulus operator%)和3

将分离条件放在每次迭代的开始处。 (Demo

像这样:

$x=0;  // I prefer to increment starting from zero.
       // This way I can use the same method inside a foreach loop on
       // zero-indexed arrays, leveraging the keys, and omit the `++` line.
echo "<div class=\"row\">";
    foreach($rows as $row){
        if($x!=0 && $x%3==0){  // if not first iteration and iteration divided by 3 has no remainder...
            echo "</div>\n<div class='row'>";
        }
        echo "<div>$row</div>";
        ++$x;
    }
echo "</div>";

这将创建:

<div class="row"><div>one</div><div>two</div><div>three</div></div>
<div class='row'><div>four</div><div>five</div><div>six</div></div>

延迟编辑,以下是针对类似情况的其他一些方法,它们将提供相同的结果:

foreach(array_chunk($rows,3) as $a){
    echo "<div class=\"row\"><div>",implode('</div><div>',$a),"</div></div>\n";
}

foreach ($rows as $i=>$v){
    if($i%3==0){
        if($i!=0){
            echo "</div>\n";
        }
        echo "<div class=\"row\">";
    }
    echo "<div>$v</div>";
}
echo "</div>";

澄清不该做的事情......

Sinan Ulker的答案将导致不必要的结果,具体取决于结果数组的大小。

以下是揭示问题的一般示例:

使用此输入数组表示您的pdo结果:

$rows=["one","two","three","four","five","six"];

每次迭代结束时思南的状况:

$i=1;
echo "<div class=\"row\">";
    foreach($rows as $row){
        echo "<div>$row</div>";
        if($i%3==0)echo "</div>\n<div class='row'>";  // 6%3==0 and that's not good here
        // 6%3==0 and will echo the close/open line after the content to create an empty, unwanted dom element
        $i++;
    }
echo "</div>\n\n";

将创建此:

<div class="row"><div>one</div><div>two</div><div>three</div></div>
<div class='row'><div>four</div><div>five</div><div>six</div></div>
<div class='row'></div>                          //<--- this extra element is not good

答案 1 :(得分:-1)

您需要添加一个新的闭包标记,并每隔3次迭代打开一个新的闭包标记。

        <div class="row">
        <?php
        $sql  = "SELECT * FROM products";
        $stmt = $conn->query($sql);
        $stmt->execute();
        $i=1;
        while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {

            ?>
            <div class="col-md-4">
                <div class="product">
                    <div class="title"><?php echo $row->pname ?></div>
                    <div class="img"><img
                            src="../product/img/<?php echo $row->pnumber ?>/<?php echo $row->pthumbnail ?>.jpg?$pop210x210$"/>
                    </div>
                    <div class="vijftien"></div>
                    <div class="deliver">Levertijd: <strong><?php echo $row->pdelivertime ?></strong>
                    </div>
                    <div class="vijf"></div>
                    <div class="other"></div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="price"><?php echo $row->pprice ?></div>
                        </div>
                        <div class="col-md-6">
                            <div class="order">
                                <button class="log_in" id="doLogin">Meer informatie</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php 
           if($i%3==0)echo "</div><div class='row'>";
           $i++; 
        } ?>

答案 2 :(得分:-1)

$ x = 0;   回声“”;

foreach($ rows作为$ row) {     / *为删除空div添加了另一个条件。* /

if ($x != 0 && $x % 3 == 0 && $x < count($rows))
{
    echo "</div>\n<div class='row'>";
}

echo "<div>$row</div>";
++$x;

}

回显“”;