Reputation: 44
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" }
]
}
]
}]
}';
Upvotes: 1
Views: 91
Reputation: 27305
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;
Upvotes: 0
Reputation: 68526
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
Upvotes: 4