0

I want to push one more value id to the content value.

I want to add Photo id

$id=$comment_fet['ID'];`

to content value

$content = $comment_fet['CONTENT_VALUE'];

Now

$content="{
"name": "ghggh",
"commentuser": "jghjhgjghj",
"content_type": "alb_comment",
"website_id": "571710720",
"id": 86,
"nodes": [],
"date": "2015-12-14T06:39:25.921Z",
"displayDate": "Mon Dec 14 2015 12:09:25 GMT+0530 (India Standard Time)",
"Like": 0,
"Unlike": 0,
"rating": 0,
"reportAbuse": 0
}"    

function get_album_comment($prefix) {
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
// print_r($request);
$id = $request->photoid;
  $sql = "select * from user_comment where SUB_ID='$id'";
$query = mysql_query($sql) or sqlerrorhandler("(" . mysql_errno() . ") " . mysql_error(), $sql, __LINE__);
//$datas = array();
while ($comment_fet = mysql_fetch_assoc($query)) {
     $content = $comment_fet['CONTENT_VALUE'];
     $id=$comment_fet['ID'];
    $datas[] = json_decode($content);

  }
echo $get_like = json_encode($datas);
}
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
user3386779
  • 6,883
  • 20
  • 66
  • 134

2 Answers2

0

Try this:

<?php
$temp=json_decode($content); // decodes it to json
$temp->id=$comment_fet['ID']; // appends photo id to it
$content=json_encode($temp); // encodes it back
?>
Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

If you want to push a new value(say photo_id) to the json string, then you can do something like this:

// your code

$comment_fet = mysql_fetch_assoc($query);
$content = $comment_fet['CONTENT_VALUE'];
$id=$comment_fet['ID'];
$datas = json_decode($content, true);
$datas['photo_id'] = $id;
$content=json_encode($datas);

// your code

Sidenote: Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Edited:

// your code

$contents = array(); // $contents array will contain all contents

while($comment_fet = mysql_fetch_assoc($query)){
    $content = $comment_fet['CONTENT_VALUE'];
    $id=$comment_fet['ID'];
    $datas = json_decode($content, true);
    $datas['photo_id'] = $id;
    $contents[] = json_encode($datas);
}

// loop through the $contents array to display all contents
for($i = 0; $i < count($contents); ++$i){
    echo $contents[$i] . "<br />";
}

// your code
Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37