[英]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
发布1
Image 1图片1
Post 2
发布2
Image 2图2
Post 3
邮政3
Image 3图3
At the moment i get this result: 目前我得到了这个结果:
Post 1
发布1
Image 1图片1
Post 1
发布1
Image 2图2
Post 1
发布1
Image 1图片1
Post 2
发布2
Image 1图片1
Post 2
发布2
Image 2图2
Post 2
发布2
Image 3图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. 试试这个(减去潜在的语言细节,因为我实际上没有运行来检查这个代码)..它基本上是一个常规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'>";
}
}
Hope that helps :) 希望有帮助:)
Getting image details from get_posts()
and removing inner foreach
loop may fix your problem. 从
get_posts()
获取图像详细信息并删除内部foreach
循环可能会解决您的问题。
Note: replace $info['something_like_post_image']
with your image field. 注意:用图像字段替换
$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'>";
//}
}
UPDATE 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.
检查以下选项以处理这种情况。
UPDATE - 2 更新 - 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'>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.