php - 是否可以关闭并添加新的在循环?
问题描述
我正在显示来自数据库的数据。目前,我的数据库中有 6 条记录,我的输出如下
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
现在我正在做的是,我必须</ul>
在 4thli
标签之后关闭标签,然后ul
在 4th之后重新开始li
。
我的预期输出是
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
可能吗?
我正在使用下面的代码
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
解决方案
只要满足您的条件,您就可以插入一个结束标签,然后是一个开始标签。在以下每第三项之后:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
输出:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
推荐阅读
- c# - 回发时的文本框行为
- python - Python中使用Numpy的卷积层
- javascript - Socket.IO 404 在本地工作,但在通过 Netlify 部署时不起作用
- javascript - 为什么 VueJs 生成评论而不是模板?
- python - 基于另一个属性的概率填充缺失值
- javascript - 当现有元素只有类,没有 ID 时,jQuery/Javascript 将唯一元素添加到其他元素
- css - bootstrap 4 scss:如何将 bootstrap 的 pb-x pm-x 值混合到自定义类中?
- c# - 如何使用 Microsoft Graph .NET SDK 检索 OneNote 页面的内容?
- react-native - 如何在另一个组件中调用一个组件的 redux 操作
- mongodb - 我如何在 NESTJS 中设置多租户