【问题标题】:PHP create a new <ul> after every fifth element in a while loopPHP 在 while 循环中每隔五个元素创建一个新的 <ul>
【发布时间】:2012-10-16 04:20:29
【问题描述】:

您好,我正在尝试将 MySQL 查询结果输出为列表,以便在每五行之后创建一个新列表。 我按照之前对我的一个类似问题提出的相同方式进行了操作,它将第五行之后的结果分开,但在每个新列表中,第一个元素与上一个列表中的最后一个元素相同。

我使用了这个代码:

/*there is a mysqli query before this*/
$i = 0;
$count = $res->num_rows;
echo '<ul>';
while($obj = $res->fetch_object()){
    $i++;
    $exturl = $obj->link;
    $extname = utf8_encode($obj->title);
    echo '<li><a class="URL" title="'.$extname.'" href="'.$exturl.'" target="_blank">'.$extname.'</a></li>';
    if($i % 5 == 0 && $i < $count){
        echo '</ul><ul><li><a class="URL" title="'.$extname.'" href="'.$exturl.'" target="_blank">'.$extname.'</a></li>';
    }
}
echo '</ul>';

问题是,我怎样才能避免重复?

【问题讨论】:

  • 究竟重复了什么?
  • i 为 1 时,i % 5 == 1 将在第二行为真。
  • 抱歉,这是一个类型错误,我会更正。

标签: php mysqli while-loop


【解决方案1】:
$args=array('orderby' => 'title','order' => 'ASC','posts_per_page'=>-1 ,'post_type'=>'Product');
$loop = new WP_Query($args);'
$counter = 1;
$reccounter=1;
$total_items = count($loop);
$ColCounter=($loop->post_count)/3;
if(!empty($loop)){ 
print '<ul style="float:left;list-style:none;" class="productList">';
while ( $loop->have_posts() ) : $loop->the_post(); 
$reccounter++;
$this_char=strtoupper(substr($loop->post->post_title,0,1));                         
if ($this_char != $last_char) { $last_char = $this_char;
$counter++;
echo '<h2 class="productTitle">'.$last_char.'</h2>';
} 
if( $ColCounter<=$reccounter){echo "</ul><ul style='float:left;list-style:none;' class='productList'>";
$reccounter=1;
}   
echo"<li class='productListpad'><a href=".get_permalink($loop->post->ID).   "title=".esc_attr($loop->post->post_title ? $loop->post->post_title :        $loop->post->ID).">".esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID)."</a></li>";
endwhile; echo"</ul>";
}wp_reset_query(); 

【讨论】:

  • 如果您想按字母顺序显示您的产品组,请使用此代码并将其插入您的 php 部分。它对我有用。例如,请参阅此代码的输出单击此处levvongroup.com/products
【解决方案2】:

解决方案是在循环中移动 if 语句:

改成这样:

while($obj = $res->fetch_object()){
$i++;
$exturl = $obj->link;
$extname = utf8_encode($obj->title);
    if($i % 5 == 0 && $i < $count){
        echo '</ul><ul><li><a class="URL" title="'.$extname.'" href="'.$exturl.'" target="_blank">'.$extname.'</a></li>';
} else { 
echo '<li><a class="URL" title="'.$extname.'" href="'.$exturl.'" target="_blank">'.$extname.'</a></li>'; 
}
}

【讨论】:

  • if 语句已经在循环中...您的代码也充满了语法错误,并且将打印前导 &lt;/ul&gt; 导致标记损坏。您还复制了 大部分 代码。如果您的if 的两个分支都执行相同的操作,您可以将该操作移到if 语句之外。 &lt;li&gt;...&lt;/li&gt; 之后的所有内容都属于 if 语句之外。
  • 我没有校对,但这就是问题所在。循环内部的 if 位于错误的位置。发生重复是因为在第 5 次迭代中创建了
  • ,然后由于 if 语句返回 true,因此还创建了第二个
  • 应该校对。本网站仅供参考,发布因语法错误而无法运行的损坏代码对任何人都没有帮助。
  • 由于 if 语句的放置而发生重复。时期。我不是来清理这些人的代码的。
  • 不,不是更好。您仍在输出无效标记的前导 &lt;ul&gt;&lt;/ul&gt;
  • 【解决方案3】:

    解决方案(取决于返回的行数)可能是在呈现列表之前array_chunk 您的结果,从而产生一种简单的分页形式。

    $results = array();
    
    while ($obj = $res->fetch_object()) {
      $results[]= $obj;
    }
    
    // Break results into sub-lists of 5 items
    $lists = array_chunk($results, 5);
    
    foreach ($lists as $list) {
      echo "<ul>"
      foreach ($list as $obj) {
        $exturl = $obj->link;
        $extname = utf8_encode($obj->title);
        echo '<li><a class="URL" title="',$extname,'" href="', $exturl, '"  target="_blank">', $extname, '</a></li>';
      }
      echo "</ul>"
    }
    

    这需要您将整个结果集加载到内存中,但它比尝试在单个循环中有条件地关闭和重新打开列表要清晰得多。

    【讨论】:

    • 我个人认为这是一个比使用 mod 条件更清洁的解决方案
    【解决方案4】:

    在条件句中添加最后一个 li 有什么意义?

    while($obj = $res->fetch_object()){
        $i++;
        $exturl = $obj->link;
        $extname = utf8_encode($obj->title);
        echo '<li><a class="URL" title="'.$extname.'" href="'.$exturl.'" target="_blank">'.$extname.'</a></li>';
        if($i % 5 == 0 && $i > 0 && $i < $count){
            echo '</ul><ul>';
        }
    }
    

    因为您已经在 if $i % 5 语句上方回显了它。

    【讨论】:

    • 非常感谢!我被另一个问题分心了,我看不到明显的问题:)
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签