在循环中构建 HTML
我试图在 PHP 中循环一个数组,同时创建一些 HTML 来输出。
当循环第一次开始时,我需要输出
然后我需要添加 3 div
然后关闭
并再次启动该过程。我已经尝试过这个,
<?php $count = 1; ?>
<?php foreach ($results as $k => $v) : ?>
<?php echo $count % 3; ?>
<?php if ($count % 3 == 0) : ?>
<li class="row">
<?php endif; ?>
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
<dd>
<?php echo $v['talent']; ?>
<ul>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
</ul>
</dd>
</dl>
</div>
</div>
<?php if ($count % 3 == 0) : ?>
</li>
<?php endif; ?>
<?php $count ++; ?>
<?php if($count >= 3) $count = 1; ?>
<?php endforeach; ?>
但是我所有的 li
都获得了类行。
期望的输出:
<li class="row">
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
<div class="grid_8">
<div class="candidate end">
<div class="model_image shadow_50"><span class="banner"></span></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
</li>
I am trying to loop through an array in PHP and at the same time, create some HTML to output.
When the loop first starts I need to output <li class="row">
I then need to add 3 divs
then close the <li>
and start the process again.
I have tried this,
<?php $count = 1; ?>
<?php foreach ($results as $k => $v) : ?>
<?php echo $count % 3; ?>
<?php if ($count % 3 == 0) : ?>
<li class="row">
<?php endif; ?>
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
<dd>
<?php echo $v['talent']; ?>
<ul>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
<li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
</ul>
</dd>
</dl>
</div>
</div>
<?php if ($count % 3 == 0) : ?>
</li>
<?php endif; ?>
<?php $count ++; ?>
<?php if($count >= 3) $count = 1; ?>
<?php endforeach; ?>
however all my li
get the class row.
Desired Output:
<li class="row">
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
<div class="grid_8">
<div class="candidate">
<div class="model_image shadow_50"></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
<div class="grid_8">
<div class="candidate end">
<div class="model_image shadow_50"><span class="banner"></span></div>
<dl>
<dt>Jessica Womersley</dt>
<dd>
actress & presenter
<ul>
<li><a href="" class="details">View Details</a></li>
<li><a href="" class="showreel">Showreel</a></li>
<li><a href="" class="shortlist">Shortlist</a></li>
</ul>
</dd>
</dl>
</div>
</div>
</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你把事情搞得太复杂了。为什么不在 foreach 中使用 for 循环呢?
像这样
You are overcomplicating things. ¿Why not just use a for loop inside the for each?
Like this