【问题标题】:Nested foreach loop (3 items + 1 / loop)嵌套的 foreach 循环(3 个项目 + 1 个/循环)
【发布时间】:2014-05-19 08:26:13
【问题描述】:

我有一个小问题,我无法通过嵌套的foreach 循环解决(PHP 初学者)。我想要实现的是这样的结构:

<div class="slide-testimonials">
   <span class="item"><img src="" /></span>
   <span class="item"><img src="" /></span>
   <span class="item"><img src="" /></span>
</div>
Sales People
<div class="slide-testimonials">
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
</div>
Freelancers
<div class="slide-testimonials">
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
</div>
Sales Managers

我现在得到的是:

<div class="slide-testimonials">
   <span class="item"><img src="" /></span>
   <span class="item"><img src="" /></span>
   <span class="item"><img src="" /></span>
</div>
Sales PeopleFreelancersSales Managers
<div class="slide-testimonials">
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
</div>
Sales PeopleFreelancersSales Managers
<div class="slide-testimonials">
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
    <span class="item"><img src="" /></span>
</div>
Sales PeopleFreelancersSales Managers

我使用的 foreach 循环是这样的:

<?php 
    $post_type = 'testimonial';
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type );
    $query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%')  AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts  {$where} ) ORDER BY p.post_date DESC";
    $results =  $wpdb->get_results( $query );   
    $arrayName = array('title1' => 'Sales People', 'title2' => 'Freelancers', 'title3' => 'Sales Managers' );               
    $count = 0;
        foreach ($results as $image) {
            $thumb = wp_get_attachment_thumb_url( $image->ID );
            $alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
                if ($count % 3 == 0) {
                    echo "<div class='slide-testimonials'>";
                }
        $count++;
?>
<span class='item'> <?php print '<img class="image" src="' . $thumb . '" alt="' . $alt . '" />' ?> </span>
<?php
    if ($count % 3 == 0) {
        echo "</div>";
            foreach($arrayName as $value) {
            print $value;
            }
    }
}
?>

如何遍历array 并在每次迭代时只打印一个值?

【问题讨论】:

    标签: php foreach


    【解决方案1】:

    在第二个if 条件下再添加一个计数器$nameCount$nameCount = 1;foreach 循环之前声明它,并在第二个 if 条件中使用下面的代码。

    if ($count % 3 == 0) {
       echo "</div>";echo $arrayName['title'.$nameCount];
       $nameCount++;
    }
    

    【讨论】:

      【解决方案2】:

      在您的第一个循环中执行此操作:

      foreach($results as $id=>$image) {
      

      然后将第二个循环替换为:

      echo $arrayName['title'.($id+1)];
      

      【讨论】:

      • 在这种情况下,我只得到数组的第一个值。我应该添加另一个计数器吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多