0

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.

11
  • Is $streamitem_imageuploaded supposed to be an array or string? You use it both ways in the foreach. Commented Mar 1, 2017 at 23:56
  • 1
    If $images is empty, then $streamitem_imageuploaded is never set. Also, what line is the error referencing and what line is that in this code? Commented Mar 1, 2017 at 23:57
  • I get a value back for the var $streamitem_imageuploaded. As said above, you use the var inconsistently. In one block as string and in another as array. Regardless, both should work if the rest of your script give back the values. I'd recommed to check: 1. $checkphoto_id['photo_id']==0 (is it really 0) 2. $photo_num 3. Check contents of $images. Test rest of execution by taking while loop away and keeping the image array and setting $photo_num = 1; Commented Mar 2, 2017 at 0:11
  • It's suppose to be a json string.. but if there is more than one image it outputs them in an array. The line error is `'streamitem_imageuploaded' => $streamitem_imageuploaded,`` inside the $json array. I'm certain its because the variable is inside the foreach loop it can't find it. Commented Mar 2, 2017 at 0:14
  • I removed the array [] off of $streamitem_imageuploaded and still get the same error. Commented Mar 2, 2017 at 0:16

2 Answers 2

0

This should work. Added some static values to simulate things. This works fine without generating undefined for your $streamitem_imageuploaded var. So problem must be in your mysql code or your other conditionals.

$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);
Sign up to request clarification or add additional context in comments.

2 Comments

when I remove if($checkphoto_id['photo_id']==0){ from my original code and add yours it works.
So the issue is with the conditions... Probably an idea to check why your $checkphoto_id['photo_id']==0 does not evaluate to true. If you benefited from the answer, please be so kind to accept it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.