循环数组问题并确定何时输入 HTML
我正在尝试循环遍历一个数组,每 3 个循环创建一个新行,但是我很难让它工作,目前我的代码如下所示,
<li class="row">
<?php for ($i = 0; $i < count($results); $i++) : ?>
<div class="grid_8">
<div class="candidate <?php if ($i % 3 == 2) echo "end"; ?>">
<div class="model_image shadow_50"></div>
<dl>
<dt><?php echo $results[$i]['first_name']; ?> <?php echo $results[$i]['surname']; ?></dt>
<dd>
<?php echo $results[$i]['talent']; ?>
<ul>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Details", array('class' => 'details')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
</ul>
</dd>
</dl>
</div>
</div>
<?php if ($i % 3 == 3) : ?>
</li><li class="row">
<?php endif; ?>
<?php endfor; ?>
但是这只是创建一行,并且我的所有结果都在其中,而它应该是 1 里,包含一类行,然后是 3 个 .grid_8 div,然后是另一行。
我哪里出错了?
I am trying to loop through an array, and every 3 loops create a new row, however I am having difficulty getting it to work, currently my code looks like this,
<li class="row">
<?php for ($i = 0; $i < count($results); $i++) : ?>
<div class="grid_8">
<div class="candidate <?php if ($i % 3 == 2) echo "end"; ?>">
<div class="model_image shadow_50"></div>
<dl>
<dt><?php echo $results[$i]['first_name']; ?> <?php echo $results[$i]['surname']; ?></dt>
<dd>
<?php echo $results[$i]['talent']; ?>
<ul>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Details", array('class' => 'details')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
</ul>
</dd>
</dl>
</div>
</div>
<?php if ($i % 3 == 3) : ?>
</li><li class="row">
<?php endif; ?>
<?php endfor; ?>
However this just creates one row, and with all my results in it, whereas it should be 1 li with a class of row, and then 3 .grid_8 divs and then another row.
Where am i going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是你的模量方程。余数永远不会达到 3,并且将是 (0,1,2,0,1,2,0,1,2) 的模式。所以你把它改为等于2。
The issue is your modulus equation. The remainder will never reach 3, and will be a pattern of (0,1,2,0,1,2,0,1,2). So you change it to equal 2.
如果您使用计数器来跟踪循环中的行位置,可能会让事情变得更容易。
If you use a counter to keep track of where you are row wise in your loop it may make things easier.
我只能假设你有一个封闭的
somewhere that you haven't shown us. Nonetheless it appears as if you're missing your final so your browser is confused.
I can only presume you have an enclosing
somewhere that you haven't shown us. Nonetheless it appears as if you're missing your final
so your browser is confused.
它不能像这样
if ($i % 3 == 3)
-$i % 3
始终在0..2
范围内。也许你
if ($i % 3 == 0)
- 它将是“每三个 $i”It just cant be like this
if ($i % 3 == 3)
-$i % 3
is always in range0..2
.Maybe you
if ($i % 3 == 0)
- it will be "every third $i"