foreach循环不显示所需的结果

时间:2012-09-09 18:20:28

标签: php mysql loops foreach

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

我正在尝试遍历我的文件夹中的图像以及数据库中的帖子,最终结果如下所示:

  

发布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'>";


    }
}

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

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

希望有所帮助:)

答案 1 :(得分:1)

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等)
  3. 更新 - 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'>";
    }