嵌套循环困难 [英] nested for loop difficulties
问题描述
我需要打印出一个封装了三个li的div,当我在一个DB中运行一个列表时。当我使用bootstrap时,我需要为.span4的第三列之后的每一行分配一个.row流体,但是我在for循环工作时遇到了一些麻烦..这里是未触及的代码: / p>
< div class =row-fluid>'
< ul class =thumbnails> ($ i = 0; $ i< count($ rows); $ i + = 1){
?>
<?php
<! - 这就是为什么按程序排序不起作用 - ID不是我所需要的 - >
< li class =span4id =<?php echo $ rows [$ i] ['Class'];?>>
< div class =thumbnail>
< img src =http://placehold.it/320x200alt =ALT NAME>
< div class =caption>
< h3><?php echo $ rows [$ i] ['Title']; ?>< / H3>
<?php if(!empty($ rows [$ i] [DateSold])){?>
<?php}?>
< p>卖家:<?php echo $ rows [$ i] ['FirstName']。 。 $行[$ i]于[名字]; ?> < / p为H.
< p>电子邮件:<?php echo $ rows [$ i] ['Email']; ?>< / p为H.
< p>成本:<?php echo $ rows [$ i] ['Cost']; ?>< / p为H.
< p>条件:<?php echo $ rows [$ i] ['BookCondition']; ?>< / p为H.
< p align =center>
<?php echo'< a href =#myModal'。$ i。'class =btn btn-primary btn-blockdata-toggle =modal>查看更多信息< / a><>
< / p>
< / div>
< / div>
< / li>
<! - Modal - >
<?php echo'< div id =myModal'。$ i。'class =modal hide fadetabindex = - 1role =dialogaria-labelledby =myModalLabel -hidden = 真 >'; ?>
< div class =modal-header>
< h3 id =myModalLabel>图书信息< / h3>
< / div>
< div class =modal-body>
< p>标题:<?php echo $ rows [$ i] ['Title']; ?> < / p为H.
< p>卖家:<?php echo $ rows [$ i] ['FirstName']。 。 $行[$ i]于[名字]; ?> < / p为H.
< p>电子邮件:<?php echo $ rows [$ i] ['Email']; ?>< / p为H.
< p>电话:<?php echo $ rows [$ i] ['PhoneNumber']; ?>< / p为H.
< p>条件:<?php echo $ rows [$ i] ['BookCondition']; ?>< / p为H.
< p>添加的日期:<?php echo $ rows [$ i] ['DateAdded']; ?>< / p为H.
< p>类:<?php echo $ rows [$ i] ['Class']; ?>< / p为H.
< p>成本:<?php echo $ rows [$ i] ['Cost']; ?>< / p为H.
< / div>
< div class =modal-footer>
< button class =btndata-dismiss =modalaria-hidden =true>关闭< /按钮>
< / div>
< / div>
<?php
}
?>
< / ul>
< / div>
在数据库每打印三次后,我需要列表项的顶级父级div在第二次读取中,似乎更像是每隔三个LI在一个单独的DIV和UL中。在这种情况下,您可以使用 array_chunk
。
<?php $ chunks = array_chunk($ rows,3);
for($ i = 0; $ i {
?> < div class =row-fluid>< ul class =thumbnails> <?php
for($ j = 0; $ j< count($ chunks [$ i]); $ j ++)
{
?>在这里做你的LI<?php
}
?> < / UL>< / DIV> <?php
}
I need to print out a div that encapsulates three li's in them as I run through a list in a DB. As I am using bootstrap, I need to div with a .row-fluid for every row after the third column of .span4, but I'm having a bit of trouble getting the for loop working.. here is the untouched code:
<div class="row-fluid">'
<ul class="thumbnails">
<?php
for($i=0; $i<count($rows); $i+=1){
?>
<!-- This is why sort by programme doesn't work - ID is not what I need -->
<li class="span4" id="<?php echo $rows[$i]['Class']; ?>">
<div class="thumbnail">
<img src="http://placehold.it/320x200" alt="ALT NAME">
<div class="caption">
<h3><?php echo $rows[$i]['Title']; ?></h3>
<?php if(!empty($rows[$i]["DateSold"])){ ?>
<span class="label label-warning" style="margin-bottom: 10px;">Book has been sold!</span>
<?php } ?>
<p>Seller: <?php echo $rows[$i]['FirstName'] . " " . $rows[$i]['LastName']; ?> </p>
<p>Email: <?php echo $rows[$i]['Email']; ?></p>
<p>Cost: <?php echo $rows[$i]['Cost']; ?></p>
<p>Condition: <?php echo $rows[$i]['BookCondition']; ?></p>
<p align="center">
<?php echo '<a href="#myModal' . $i . '" class="btn btn-primary btn-block" data-toggle="modal">View More information</a>' ?>
</p>
</div>
</div>
</li>
<!-- Modal -->
<?php echo'<div id="myModal' . $i . '" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'; ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Book information</h3>
</div>
<div class="modal-body">
<p>Title: <?php echo $rows[$i]['Title']; ?> </p>
<p>Seller: <?php echo $rows[$i]['FirstName'] . " " . $rows[$i]['LastName']; ?> </p>
<p>Email: <?php echo $rows[$i]['Email']; ?></p>
<p>Phone: <?php echo $rows[$i]['PhoneNumber']; ?></p>
<p>Condition: <?php echo $rows[$i]['BookCondition']; ?></p>
<p>Date added: <?php echo $rows[$i]['DateAdded']; ?></p>
<p>Class: <?php echo $rows[$i]['Class']; ?></p>
<p>Cost: <?php echo $rows[$i]['Cost']; ?></p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
<?php
}
?>
</ul>
</div>
I need that top parent div of row-fluid surrounding the list item after every third print from the DB..
On second readthrough, it seems more like you want every three LIs in a seperate DIV and UL. In that case, you can use array_chunk
.
<?php $chunks = array_chunk($rows, 3);
for ($i = 0; $i<count($chunks); $i++)
{
?> <div class="row-fluid"><ul class="thumbnails"> <?php
for ($j = 0; $j<count($chunks[$i]); $j++)
{
?> do your LIs in here <?php
}
?> </ul></div> <?php
}
这篇关于嵌套循环困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!