dongshou9878 2014-05-19 08:26
浏览 41
已采纳

嵌套的foreach循环(3项+ 1 /循环)

I have a small issue I can't figure out (PHP beginner) with a nested foreach loop. What I want to achieve is a structure like this:

<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

What I get now is this:

<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

The foreach loop I'm using is this:

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

How can I loop through the array and print only one value on each iteration?

  • 写回答

2条回答 默认 最新

  • duan198409 2014-05-19 08:31
    关注

    Add one more counter $nameCount increase this in second if condition. $nameCount = 1; declare this in before foreach loop and use below code in second if condition.

    if ($count % 3 == 0) {
       echo "</div>";echo $arrayName['title'.$nameCount];
       $nameCount++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?