1

hello i want to use my mysql result to build the json objects. my first query is fetching parent menu and another query is fetching child menu so i am going to put my child menu result under the parent menu.. i want to know what is wrong with my code and how to correct

<?php
    $selectparentMenu=mysql_query("SELECT `id`,`item_name`,`menu_type`,`parent` FROM `epic_master_menu` where parent=0");
    //echo $myfetch=mysql_fetch_array($selectMenu);
        if(mysql_num_rows($selectparentMenu)>1) {
    while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
         //echo 'parent id is' .$parentId=$fetchparentMenu['id']. '<br/>';
        // echo 'parent name is' .$parentId=$fetchparentMenu['item_name']. '<br/>';


         $selectchildMenu=mysql_query("SELECT `id` , `item_name` , `menu_type` , `parent` FROM `epic_master_menu`
    WHERE parent >0 AND `menu_type` = 'item' AND `parent` ='".$fetchparentMenu['id']."'");
    if(mysql_num_rows($selectchildMenu)>1) {
         while($fetchchildMenu=mysql_fetch_array($selectchildMenu)) {

             $textjson = '{
    "dataSource": [{
            "id": "", "text": "Select All", "expanded": "true", "spriteCssClass": "rootfolder", "items": [
                {
                    "id": "'.$fetchparentMenu["id"].'", "text": "'.$fetchparentMenu["item_name"].'", "expanded": true,"spriteCssClass": "folder", "items": [
                        { "id": "'.$fetchchildMenu["id"].'", "text": "'.$fetchparentMenu["item_name"].'", "spriteCssClass": "html" },
                        { "id": "'.$fetchchildMenu["id"].'", "text": "'.$fetchparentMenu["item_name"].'", "spriteCssClass": "html" },
                        { "id": "'.$fetchchildMenu["id"].'", "text": "'.$fetchparentMenu["item_name"].'", "spriteCssClass": "image" }
                    ]
                }
            ]
        }]
    }';

             }  }

         /* $fetchMenu['item_name'];
           $fetchMenu['menu_type'];
           $fetchMenu['parent'];*/
        //print_r($fetchMenu);
        } }

// my json data is

 $textjson = '{
"dataSource": [{
        "id": 1, "text": "My Documents", "expanded": "true", "spriteCssClass": "rootfolder", "items": [
            {
                "id": 2, "text": "Project", "expanded": true,"spriteCssClass": "folder", "items": [
                    { "id": 3, "text": "about.html", "spriteCssClass": "html" },
                    { "id": 4, "text": "index.html", "spriteCssClass": "html" },
                    { "id": 5, "text": "logo.png", "spriteCssClass": "image" }
                ]
            },
            {
                "id": 6, "text": "New Web Site", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 7, "text": "mockup.jpg", "spriteCssClass": "image" },
                    { "id": 8, "text": "Research.pdf", "spriteCssClass": "pdf" }
                ]
            },
            {
                "id": 9, "text": "Reports", "expanded": true, "spriteCssClass": "folder", "items": [
                    { "id": 10, "text": "February.pdf", "spriteCssClass": "pdf" },
                    { "id": 11, "text": "March.pdf", "spriteCssClass": "pdf" },
                    { "id": 12, "text": "April.pdf", "spriteCssClass": "pdf" }
                ]
            }
        ]
    }]
}';
2
  • 1
    why not make use of json_encode() ? Commented Feb 25, 2014 at 8:51
  • would you please clarify it i mean where and how should i implement Commented Feb 25, 2014 at 8:53

2 Answers 2

4

Instead of creating your own version.. you could simply make use of json_encode()

while($fetchchildMenu=mysql_fetch_array($selectchildMenu))
 {
      $somearr[]=$fetchchildMenu;
 }

$jsonData = json_encode($somearr);
echo $jsonData; //<---- Prints your JSON data
Sign up to request clarification or add additional context in comments.

6 Comments

i have my own json format and i just want to insert my query record on it
so format the array to your format and then use json_encode on it
@Sachin, Creating your own version is a pretty headache. Do like Robert mentioned.
this is my json formta
i know how to encode the array into json.. but i am actuly not getting how to build an array that will display in my json format
|
0

Its much easier to use the included json functions from PHP. Here you can use json_encode to create a json string from an array.

$childArray = array();

while($fetchchildMenu=mysql_fetch_array($selectchildMenu)) {
   $childArray[] = array(
       'id' => $fetchchildMenu['id'],
       'text' => $fetchchildMenu['text']
   );
}

$jsonDataChilds = json_encode($childArray);
echo $jsonDataChilds;

3 Comments

i have edited my question i have put my json format there so if i want to use array then how i need to build the array by using the same format of json.
To avoid mistakes in your schema its easier to build an array and convert it to the final json string using the json functions. Don't reinvent the wheel its not necessary.
i just want to know how to build an array which will show me my required json format..

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.