foreach循环未显示所需结果


foreach loop not displaying desired result

大家好,谢谢大家抽出时间。我的问题是关于。

我正试图循环浏览我文件夹中的图像以及数据库中的帖子,最终结果如下:

帖子1
图像1

后2
图像2

后3
图像3

目前我得到的结果是:

帖子1
图像1

张贴1
图像2

帖子1
图像1

帖子2
图像1

后2
图像2

后2
图像3

我不想要这个结果。

以下是我的代码:

    $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'>";

    }
}

谢谢你抽出时间。

试试这个(减去潜在的语言细节,因为我实际上并没有运行来检查这个代码)。。它基本上是一个常规的for循环,而不是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'>";
    }
}

希望能有所帮助:)

get_posts()获取图像详细信息并删除内部foreach循环可能会解决您的问题。

注意:将$info['something_like_post_image']替换为您的图像字段

$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'>";
    //}
}

更新

/*
 * 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'>";
    }
}

注意:你应该根据你的独特形象与每个帖子保持联系;否则,你将无法在你的帖子中获得独特的图片,同时列出。签出以下选项以处理这种情况

  1. 您可以将图片名称保存在数据库中(对于每个帖子,您可以直接从数据库中获取图片名称)
  2. 为您的图像使用命名约定(对于帖子#1,使用唯一的图像名称(比如image_1,对于帖子#2 image_2等)

更新-2

如果你正在寻找一个循环通过图像(没有任何条件),使用以下代码

/*
 * 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'>";
}