【问题标题】:Is it possible to close </ul> and add new <ul> in the loop?是否可以关闭 </ul> 并在循环中添加新的 <ul> ?
【发布时间】:2019-11-24 19:04:55
【问题描述】:

我正在显示来自数据库的数据。目前,我的数据库中有 6 条记录,我的输出结果如下:

<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>

现在我要做的是,我必须在第 4 个li 标签之后关闭&lt;/ul&gt; 标签,然后在第 4 个li 之后开始新的ul

我的预期输出是

<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 jquery html css html-lists


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这是一个简单的例子。希望能帮到你。

      if ($tyler_query->have_posts()) {
      
          $index = 0;
          $check = 6;
          ?>
      
          <ul>
          <?php while ($tyler_query->have_posts()) {
      
              $tyler_query->the_post(); ?>
      
              <li><?php echo 'some_value' ?></li>
      
              <?php if ($index % $check === 0 ) { ?>
      
              </ul><ul>
      
              <?php }
      
              $index++;
          } ?>
          </ul>
      
      <?php } ?>
      

      【讨论】:

        【解决方案3】:

        如果您回显您希望在浏览器中看到的 HTML 标记/输出,您的代码将起作用。

        <?php
        if ($tyler_query->have_posts()) {
            $index = 0;
            $check = 0;
            $first4 = 0;
        
            while ($tyler_query->have_posts()) {
        
                $tyler_query->the_post();
                $output = "whatever the output object is";
        
                if ($index < 4) {
                    if ($first4 == 0) {
                        echo '<ul>';
                        $first4 = 1; // increment so that the this if block wont trigger again
                    }
                    echo '<li>' . $output . '</li>';
                    // increment so that the next if block trigger once
                    if ($first4 == 4) {
                        echo  '</ul>';
                    }
                    $first4++;
                }
                if ($index >= 4){
                    if ($check == 0) {
                        echo '<ul>';
                        $check = 1;
                    }
                    // assuming you want to have the rest of the data in this block.
                    // data 5 and above
                    else {
                        echo '<li>' . $output . '</li>';
                    }
                }
                    $index++;
                }
            echo '</ul>';
            } 
        ?>
        

        【讨论】:

        • 回声将如何在您的答案中发挥作用?在 echo 之前关闭 PHP
        • 对不起,我没有仔细阅读问题并做出很多假设,现在我再次阅读了问题,我看到您使用了多个“php标签”块并且我很困惑为什么。我重新做了,所以它现在应该可以工作了
        • 让我检查你的答案。给我一些时间
        • 这一行我没看懂 $output = "whatever the output object is";我应该在这里添加我的 li 标签代码
        • 此变量可以是您从正在获取的数据库行创建的基础对象,也可以是您要输出的字符串(例如姓名、电子邮件等)
        猜你喜欢
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多