0

i was unable to return json in the following format as it required for supersized image gallery pluggin.

              [ {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-1.jpg' title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-1.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'},
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-2.jpg', title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-2.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'},  
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-3.jpg', title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-3.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'},
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-1.jpg', title : 'Image Credit: Colin Wojno', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-1.jpg', url : 'http://www.nonsensesociety.com/2011/03/colin/'},
                                                    { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', title: 'Image Credit: Brooke Shaden', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg', url: 'http://www.nonsensesociety.com/2011/06/brooke-shaden/' },  
                                                  { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-2.jpg', title: 'Image Credit: Maria Kazvan', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-2.jpg', url: 'http://www.nonsensesociety.com/2011/04/maria-kazvan/' },
                                                    { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-3.jpg', title: 'Image Credit: Maria Kazvan', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-3.jpg', url: 'http://www.nonsensesociety.com/2011/04/maria-kazvan/' },
                                                    { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-1.jpg', title: 'Image Credit: Colin Wojno', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-1.jpg', url: 'http://www.nonsensesociety.com/2011/03/colin/' },                 
                                                    {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-3.jpg', title : 'Image Credit: Brooke Shaden', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-3.jpg', url : 'http://www.nonsensesociety.com/2011/06/brooke-shaden/'}

I am tried with following code but not succeded.

$sql1 = "select file,title from multi_uploads where object_id='$postid'";

                    $result=mysql_query($sql1 ,$conn);

                            if (!$result) 
                            {
                                die('Invalid query: ' . mysql_error());
                            }



                                                       $finaloutput=array();

                            while($row=mysql_fetch_row($result))
                            {
                                                                   $output=array();

                   $image='image : \'http://xxxxxxxxx.in/sample/newedp/admin/files/'.$row[0].'\'';
                     $title='title : \''.$row[1].'\'';
                        $thumb='thumb : \'http://xxxxxxxxxxxx.in/sample/newedp/admin/files/thumbnail/'.$row[0].'\'';
                       $url='url : \'http://xxxxxxxxxxxxx.in/sample/newedp/admin/files/'.$row[0].'\'';
                       $onerow='{ '.$image.','.$title.','.$thumb.','.$url.'}';
                    array_push($finaloutput,json_encode($onerow));  

                             echo json_encode($finaloutput);

give me the sample code to print json in the above format.

3
  • 1
    try to prepare array properly. then use json_encode. Commented Jan 16, 2014 at 7:42
  • Have a look at some of the example here & try again. Commented Jan 16, 2014 at 7:43
  • take echo json_encode($finaloutput); outside of while loop Commented Jan 16, 2014 at 8:07

3 Answers 3

1
$output = [];
while($row = mysql_fetch_row($result)) {
  $array = [];
  $array["image"] = "http://panacya.in/sample/newedp/admin/files/{$row[0]}";
  $array["title"] = $row[1];
  ...
  $output[] = $array;
}
echo json_encode($output);
Sign up to request clarification or add additional context in comments.

Comments

0

Try

json_encode($finaloutput,JSON_UNESCAPED_SLASHES)

Instead of

json_encode($finaloutput);

1 Comment

Unfortunately this doesn't address the core problem of trying to build JSON from strings manually and then shoving the strings (which aren't even valid JSON) into an array which is later json_encoded.
0

The core problem is that the code is not using json_encode correctly - json_encode takes a PHP object graph (arrays and values, as displayed by print_r) and encodes that into JSON.

Instead, the code posted tries to build JSON manually (oops!) then tries to json_encode the manually built JSON (oops!) and then tries to json_encode an array of all that junk (oops!). The end result is that, while it will be valid JSON thanks to the final json_encode, it will look nothing like the intended format.

Instead, the code prepare the object graph first and only run json_encode on it once at the end.

$items = array();

for (each row) {
  // DO NOT build the "JSON" for the item manually; it will be
  // encoded later when the object graph is traversed by json_encode.
  // (Look how much cleaner the code already looks!)
  $item = array(
      "image" => "http://panacya.in/sample/newedp/admin/files/".$row[0],
      "title" => "".$row[1]
  );

  // Add the item to the object graph, but DO NOT encode it; it will be
  // encoded later when the object graph is traversed by json_encode.
  // (If the item is encoded here then the later call to json_encode
  //  will be encoding an array of strings and not an array objects.)
  push_array($items, $item);
}

// At the end, use json_encode ONCE on the entire object graph root.
// All the reachable values (as seen with print_r) will be encoded as JSON.
$result = json_encode($items);

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.