简体   繁体   中英

foreach loop not displaying desired result

Hi guys and thank you for your time. My question is regarding.

I am trying to loop over images in my folder along with a post in the database with the end result looking like this:

Post 1
Image 1

Post 2
Image 2

Post 3
Image 3

At the moment i get this result:

Post 1
Image 1

Post 1
Image 2

Post 1
Image 1

Post 2
Image 1

Post 2
Image 2

Post 2
Image 3

I do not want this result.

Below is my code:

    $post_info = get_posts();

foreach ($post_info as $info){
   $photos = glob('design/img/*');
   foreach($photos as $photo) {
    echo " <a href='feed.php?pid=".$info['post_id']." ' > 
           <div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2> 
        <h3 style='color: black'>    ".$info['body']." </h3> </div> </a>";
  echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";


    }
}

Thanks for your time.

Try this out (minus potential language specifics since I didn't actually run to check this code).. It's basically a regular for loop instead of a foreach.

$post_info = get_posts();
$photos = glob('design/img/*');

if (count($post_info) === count($photos)) { // According to your requirement, the counts would be the same
    $count = count($post_info);
    for ($i = 0; $i < $count; $i++) {
        $info = $post_info[$i];
        $photo = $photos[$i];
        echo " <a href='feed.php?pid=".$info['post_id']." ' > <div style='background:#FFF5C3'> <br> <h2> ".$info['person_mentioned']." </h2> 
        <h3 style='color: black'>    ".$info['body']." </h3> </div> </a>";
        echo " <img src='{$photo}' width='285px' height='200px' style='border: 5px solid black'>";
    }
}

Hope that helps :)

Getting image details from get_posts() and removing inner foreach loop may fix your problem.

Note: replace $info['something_like_post_image'] with your image field.

$post_info = get_posts();

foreach ($post_info as $info) {
    //$photos = glob('design/img/*');
    //foreach ($photos as $photo) {
    echo " <a href='feed.php?pid=" . $info['post_id'] . " ' > 
           <div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2> 
        <h3 style='color: black'>    " . $info['body'] . " </h3> </div> </a>";
    echo " <img src='" . $info['something_like_post_image'] . "' width='285px' height='200px' style='border: 5px solid black'>";
    //}
}

UPDATE

/*
 * If your images have any naming convention like
 * imageFileName = "image_{POST_ID}.jpg"
 * then you can use below code (NO DATABASE ENTRY REQUIRED)
 * (ie, For post #1 image file would be "image_1.jpg";
 * and for post #2 image file would be "image_2.jpg")
 */

$post_info = get_posts();

foreach ($post_info as $info) {

    //filename = image_1.jpg or image_2.jpg or...
    $photoFileName = 'design/img/' . 'image_' . $info['post_id'] . '.jpg';

    if (file_exists($photoFileName)) {
        echo " <a href='feed.php?pid=" . $info['post_id'] . " ' > 
                           <div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2> 
                        <h3 style='color: black'>    " . $info['body'] . " </h3> </div> </a>";
        echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
    }
}

NOTE: You should have to keep a relation with each post against your unique image; otherwise you will not be able to get that unique image with your post, while listing. Checkout below options to handle this situation.

  1. You can keep image name in database (for each post, you can get your image name directly from database)
  2. Use a naming convention for your images (for post #1 use unique image name (say image_1, for post #2 image_2 etc)

UPDATE - 2

If you are looking for a cycle-through images (without any condition), use below code

/*
 * If you are looking for a solution that cycles each images
 * along with each post, try this one
 */

$post_info = get_posts();
$photos = glob('design/img/*');

$numPhotos = count($photos) + 1;

//assuming your post# starts with 1
$imageId = 1;
foreach ($post_info as $info) {

    //cycling
    if ($imageId % $numPhotos === 0) {
        $imageId = 1;
    }

    $photoFileName = 'design/img/' . 'image_' . $imageId++ . '.jpg';

    //no need of this checking, since you are cycling
    //if (!file_exists($photoFileName)) {
    //    $photoFileName = 'path/to/default/image.jpg';
    //}

    echo " <a href='feed.php?pid=" . $info['post_id'] . " ' > 
                               <div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2> 
                            <h3 style='color: black'>    " . $info['body'] . " </h3> </div> </a>";
    echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}

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