简体   繁体   中英

Nested foreach loop (3 items + 1 / loop)

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?

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

Do this with your first loop:

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

Then replace your second loop with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM