简体   繁体   中英

pass variables not in database row through json array

I have a bit of code I would like to pass through my json array that is not contained in the database $row. So i tried to set a custom variable and that didn't work. Is there a way to do this so the below content gets sent through with the array?

Here is what I tried

PHP

if ($photo_numff == 1) {
    $streamitem_uploadimage_count =" Uploaded new image";
} else if($photo_numff > 1) { 
    $streamitem_uploadimage_count =" Uploaded ".$photo_numff." new images";
} else {
}   

JSON array

$rowcount = mysqli_num_rows($result);
$json = array(
    'posts' => array(),
    'count' => $rowcount
);

while ($row = mysqli_fetch_array($result)) {    
    $posts[] = array(
        //Post information and ids
        'streamitem_id' => $row['streamitem_id'], // post id
        'streamitem_uploadimage_count' => $streamitem_uploadimage_count,
    );
    $rowcount++; 
}
$json['posts'] = $posts;
echo json_encode($json);

I can then take this through my ajax "+response['streamitem_uploadimage_count']+"

I have also tried array_push($json['posts'], array( 'streamitem_uploadimage_count' => $streamitem_uploadimage_count, ) ); but doesn't work

I have now got this working by adding

'streamitem_uploadimage_count' => $streamitem_uploadimage_count 

within the $Json array also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM