doujiao9574 2017-03-01 23:52
浏览 75
已采纳

从foreach循环内部调用变量以放入数组内部

What I'm trying to do is get my $streamitem_imageuploaded value so I can pass it into my $json array. Has this got anything to do with a nested loop? When I send my json data back currently, all I get is undefined variable $streamitem_imageuploaded with the below code.

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>';
        }
    }
}

Store the outputted value inside my $json array

$json = array(
    'posts' => array(),
    'count' => $rowcount,
    'commentlinktoggle' => $sendcommentlinktoggle,
    'streamitem_formholder' => $streamitem_formholder,
    'stopcommentsbutton' => $stopcommentsbutton,
    'streamitem_uploadimage_count' => $streamitem_uploadimage_count,
    'streamitem_imageuploaded' => $streamitem_imageuploaded,
);

UPDATE

Edit changed and still getting same result

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>';
}

This code works in a standard php page no issues. just not when trying to call the variable from inside an array.

  • 写回答

2条回答 默认 最新

  • dtewnsdf47253 2017-03-02 03:24
    关注

    All i needed to do was to add an array outside the foreach loop and then call this within my $json array

        $streamitem_imageuploaded = array();
    

    I used this when researching for my answer. Return all values stored in var outside foreach loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀