【发布时间】:2017-06-15 01:43:52
【问题描述】:
我想要实现的是将所有可用的图片从使用相同 photo_title/timestamp 发布的用户照片中获取到一个数组中,并将图片的数量发布到 streamitem_id 下的指定帖子中。
目前帖子按应有的方式插入,但如果我一次上传多张图片,帖子中只会显示一张。我将如何在已经构建的数组“posts”中制作$posts['streamitem_imageuploaded']一个数组。
这是我目前所拥有的。
$call = "SELECT *
FROM streamdata m
JOIN user t1 ON m.streamitem_creator = t1.id
JOIN user t2 ON m.streamitem_target = t2.id
WHERE
m.streamitem_id > ".$_GET['streamitem_id']."
ORDER BY m.streamitem_timestamp DESC";
$check1 = mysqli_query($mysqli, $call) or die(mysqli_error($mysqli));
$json = array(
'posts' => array(),
);
while($row = mysqli_fetch_array($check1)){
$posts = array();
$posts['streamitem_id'] = $row['streamitem_id'];
//start of image code. only ouputs one image in each 'posts' array.
$sqlhhh = "SELECT * FROM userphotos WHERE photo_name='".$row['photo_title']."' AND photo_ownerid='".$row['streamitem_creator']."' AND photo_datetime='".$row['streamitem_timestamp']."' ORDER BY photo_id DESC";
$resulthhh = mysqli_query ($mysqli,$sqlhhh)or die(mysqli_error($mysqli));
$photo_num=mysqli_num_rows($resulthhh);
$images = array();
while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
$rowhhh = mysqli_fetch_assoc($resulthhh);
$images[] = $rowhhh['photo_imagedata'];
}
foreach ($images as $ima){
if($photo_num==1){
$posts['streamitem_imageuploaded']='<img src="data:image/jpeg;base64,'. base64_encode($ima) .'" />';
}else{
$posts['streamitem_imageuploaded']='<img class="stream_images" style="width:235px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($ima) .'" />';
}
}
$json['posts'][] = $posts;
}
}
echo json_encode($json);
【问题讨论】:
标签: php arrays json loops foreach