hackalive Posted January 15, 2012 Share Posted January 15, 2012 Hi guys, I need to know how to json_encode a mysql output, so far the PHP ref manual for json_encode is not proving very helpful. Any help is much appreciated. Quote Link to comment Share on other sites More sharing options...
SergeiSS Posted January 15, 2012 Share Posted January 15, 2012 You mean that this explanation with examples http://ru2.php.net/manual/en/function.json-encode.php is not enough for you? OK, what else do you like to know? Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 15, 2012 Author Share Posted January 15, 2012 Im looking how to output json like this from PHP {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, {"id": "SaveAs", "label": "Save As"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ] }} REF: http://json.org/example.html Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2012 Share Posted January 15, 2012 And where exactly are you stuck? Code would be helpful. The general idea would be something like: $out = array(); while ($row = $result->fetch_assoc()) { $out[] = $row; } echo json_encode($out); Obviously you would need to format the $out array into whatever format you need though. Quote Link to comment Share on other sites More sharing options...
SergeiSS Posted January 15, 2012 Share Posted January 15, 2012 I hope this code helps you // your data are inserted into a string $s='{"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, {"id": "SaveAs", "label": "Save As"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ] }} '; // process and output a decoded info echo '<pre>'. print_r( json_decode( $s, true ), true). '</pre>'; Instead of printing you may assign it to an array and process it. PS. In my browser I see this Array ( [menu] => Array ( [header] => SVG Viewer [items] => Array ( [0] => Array ( [id] => Open ) [1] => Array ( [id] => OpenNew [label] => Open New ) [2] => [3] => Array ( [id] => ZoomIn [label] => Zoom In ) [4] => Array ( [id] => ZoomOut [label] => Zoom Out ) [5] => Array ( [id] => OriginalView [label] => Original View ) [6] => [7] => Array ( [id] => Quality ) [8] => Array ( [id] => Pause ) [9] => Array ( [id] => Mute ) [10] => [11] => Array ( [id] => Find [label] => Find... ) [12] => Array ( [id] => FindAgain [label] => Find Again ) [13] => Array ( [id] => Copy ) [14] => Array ( [id] => CopyAgain [label] => Copy Again ) [15] => Array ( [id] => CopySVG [label] => Copy SVG ) [16] => Array ( [id] => ViewSVG [label] => View SVG ) [17] => Array ( [id] => ViewSource [label] => View Source ) [18] => Array ( [id] => SaveAs [label] => Save As ) [19] => [20] => Array ( [id] => Help ) [21] => Array ( [id] => About [label] => About Adobe CVG Viewer... ) ) ) ) Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 17, 2012 Author Share Posted January 17, 2012 I want to know how I can construct a JSON array like what I showed above by using json_encode which will be populated from multiple mysql queries so how do i do the strucutre like that? and make it? Do i manually enter the [ and { etc? Quote Link to comment Share on other sites More sharing options...
SergeiSS Posted January 17, 2012 Share Posted January 17, 2012 Do you ever heard about helps? For example, about this one: http://php.net/manual/en At that site there are descriptions for some functions, like json_encode and json_decode. Try to read it and you understand. I hope that you understand Also I hope that my previous answer can help you. Quote Link to comment Share on other sites More sharing options...
trq Posted January 17, 2012 Share Posted January 17, 2012 I want to know how I can construct a JSON array like what I showed above by using json_encode which will be populated from multiple mysql queries so how do i do the strucutre like that? and make it? Do i manually enter the [ and { etc? No you don't have to put the [ and { in there manually. json_encode encodes php arrays and objects into json notation. Without seeing your current data structure we can't be of anymore help. Not that we are here to write code for people in the first place. Have you actually tried to have a go at this yourself? Why not post your problematic code? Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 20, 2012 Author Share Posted January 20, 2012 How can there be problamatic code when no one so far can exmplain how to constuct the data like the samples I have provided. Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 20, 2012 Share Posted January 20, 2012 How can there be problamatic code when no one so far can exmplain how to constuct the data like the samples I have provided. thorpe already did just that - $out = array(); while ($row = $result->fetch_assoc()) { $out[] = $row; } echo json_encode($out); You simply pass a PHP array or object to json_encode, and it gives you JSON. There's nothing else to it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.