Jump to content

json_encode a mysqli output


hackalive

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...
                        )

                )

        )

)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.