我尝试做的是获取$streamitem_imageuploaded
值,以便将其传递到$json
数组中。这与嵌套循环有什么关系吗?当我当前发回我的json数据时,我得到的是未定义的变量$streamitem_imageuploaded
,代码如下。
if($checkphoto_id['photo_id']==0){
$sqlhhh = "SELECT * FROM userphotos WHERE photo_name='".$checkphoto_id['photo_title']."' AND photo_ownerid='".$checkphoto_id['streamitem_creator']."' AND photo_datetime='".$checkphoto_id['streamitem_timestamp']."' ORDER BY photo_id ASC";
$resulthhh = mysqli_query ($mysqli,$sqlhhh)or die(mysqli_error($mysqli));
$photo_num=mysqli_num_rows($resulthhh);
$images = array();
while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
$imageArray = array(
'data' => $rowhhh['photo_imagedata'],
'photo_streamitem_id' => $rowhhh['photo_streamitem_id'],
'id' => $rowhhh['photo_id']
);
$images[] = $imageArray;
}
foreach ($images as $image) {
if($photo_num==1){
$streamitem_imageuploaded='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}else{
$streamitem_imageuploaded[]='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}
}
}
将输出的值存储在我的$ json数组
中$json = array(
'posts' => array(),
'count' => $rowcount,
'commentlinktoggle' => $sendcommentlinktoggle,
'streamitem_formholder' => $streamitem_formholder,
'stopcommentsbutton' => $stopcommentsbutton,
'streamitem_uploadimage_count' => $streamitem_uploadimage_count,
'streamitem_imageuploaded' => $streamitem_imageuploaded,
);
更新
编辑已更改且仍获得相同的结果
if($photo_num==0){
$streamitem_imageuploaded='';
}
if($photo_num==1){
$streamitem_imageuploaded='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}
if($photo_num>1){
$streamitem_imageuploaded[]='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}
此代码在标准的php页面中工作没有问题。只是在尝试从数组内部调用变量时。
答案 0 :(得分:0)
这应该有效。添加了一些静态值来模拟事物。这样可以正常运行,而不会为$ streamitem_imageuploaded var生成未定义的内容。所以问题必须出在你的mysql代码或其他条件中。
$rowhhh =1;
$photo_num = 1;
$images = array();
if ($rowhhh == 1) {
$imageArray = array(
'data' => $rowhhh['photo_imagedata'],
'photo_streamitem_id' => $rowhhh['photo_streamitem_id'],
'id' => $rowhhh['photo_id']
);
$images[] = $imageArray;
}
foreach ($images as $image) {
if($photo_num==1){
$streamitem_imageuploaded='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}else{
$streamitem_imageuploaded[]='<a href="photo.php?pid='.$image['id'].'&streamitem_id='.$image['photo_streamitem_id'].'"><img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
}
}
$json = array(
'posts' => array(),
'count' => $rowcount,
'commentlinktoggle' => $sendcommentlinktoggle,
'streamitem_formholder' => $streamitem_formholder,
'stopcommentsbutton' => $stopcommentsbutton,
'streamitem_uploadimage_count' => $streamitem_uploadimage_count,
'streamitem_imageuploaded' => $streamitem_imageuploaded,
);
print_r($json);
答案 1 :(得分:0)
我需要做的就是在foreach循环外添加一个数组,然后在我的$ json数组中调用它
$streamitem_imageuploaded = array();
我在研究我的答案时使用了这个。 Return all values stored in var outside foreach loop