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

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.

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

                )

        )

)

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

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

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 -

  Quote

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.