每3条记录后新的包含div [英] New containing div after every 3 records

查看:115
本文介绍了每3条记录后新的包含div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对于我的自学项目,我必须用bootstrap制作一个产品页面,每创建一个记录后,我必须创建一个新行,并再次显示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_inid =doLogin>更多信息< / button>
< / div>
< / div>
< / div>
< / div>
< / div>
<?php}?>
< / div>

我曾经访问并研究过其他问题,但我并没有真正了解他们是如何做的以及我如何在我的代码中实现正确的方法。

解决方案

正如tadman在您的问题的评论中所述。最好的方法应该使用模数运算符)与 3



将分离条件放在每个迭代。 ( Demo



像这样:

  $ x = 0; //我更喜欢从零开始增加。 
//这样我可以在
//零索引数组的foreach循环中使用相同的方法,利用这些键,并省略`++`行。
echo< div class = \row \>;
foreach($ rows as $ row){
if($ x!= 0&& $ x%3 == 0){//如果不是第一次迭代和迭代除以3就没有余数...
echo< / div> \\\
< div class ='row'>;
}
echo< div> $ row< / div>;
++ $ x;
}
echo< / div>;

这将创建:

 < div class =row>< div>一个< / div>< div>两个< / div>< div>三< / 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> \\\
;
}



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






澄清不该做的事情...



根据结果数组的大小,Sinan Ulker的答案会导致不想要的结果。



这是一个

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

  $行= [ 一, 二, 三, 四, 5, 6]; 

每次迭代结束时Sinan的条件:

  $ I = 1; 
echo< div class = \row \>;
foreach($ rows为$ row){
echo< div> $ row< / div>;
if($ i%3 == 0)echo< / div> \\\
< div class ='row'>; // 6%3 == 0,这在这里不是很好
// 6%3 == 0并且会在内容创建一个空的不需要的dom元素后回显close / open行
$ i ++ ;
}
echo< / div> \\\
\\\
;

会创建这个:

 < div class =row>< div>一个< / div>< div>两个< / div>< div>三< / div>< / div> 
< div class ='row'>< div> four< / div>< div> five< / div>< div> six< / div>< / div>
< div class ='row'>< / div> //< ---这个额外元素不好


I would like to create a new containing <div> after 3 results, using PDO result loop.

For my self-study-project I have to made a product page with bootstrap and after every 3rd record I have to make a new row and show again 3 col-md-4's, etc, etc.

Now I have this as my code:

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

I have visited and studied other questions but I do not really get the idea of how they are doing it and how I can implement the correct method into my code.

解决方案

As tadman stated in the comment under your question. The best approach should use a modulus operator (%) with 3.

Place your separating condition at the start of each iteration. (Demo)

Like this:

$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>";

This will create:

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


Late Edit, here are a couple of other methods for similar situations which will provide the same result:

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

or

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


To clarify what NOT to do...

Sinan Ulker's answer will lead to an unwanted result depending on the size of your result array.

Here is a generalized example to expose the issue:

Using this input array to represent your pdo results:

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

Sinan's condition at the end of each iteration:

$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";

Will create this:

<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

这篇关于每3条记录后新的包含div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆