首页 > 解决方案 > 是否可以关闭并添加新的

问题描述

我正在显示来自数据库的数据。目前,我的数据库中有 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   }?>

标签: phpjqueryhtmlcsshtml-lists

解决方案


只要满足您的条件,您就可以插入一个结束标签,然后是一个开始标签。在以下每第三项之后:

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

推荐阅读