i need to iterate in js(no framework) over a json object generated in php with the follow structure:
{
EDITED: I DID HAVE AN ERROR, SORRY
}
I search for a solution without any js framework.
¿how to get, for example,
lvl1a.lvl2a.a
lvl1a.lvl2a.b
lvl1a.lvl2b.a
lvl1a.lvl2b.b
...
?
Thanks
more information:
I DID HAVE AN ERROR IN PREV CODE!!! SORRY
Function: Populate two select, “select1” and “select2”, with option=Category optgroup=CategoryParent
$toJson=array();
$selectname=array("select1","select2");
for($i=0;$i<2;$i++){
$sql="SELECT idCategory as v,name as l,(select name from categories where idCategory=C.idCategoryParent) as p FROM categories C WHERE idBanner".($i+1)." is NULL order by idCategoryParent, `order`";
$result =$ocdb->query($sql);
$nameIdx = "";
while ($row=mysql_fetch_assoc($result)){
$first=mysql_field_name($result, 0);
$second=mysql_field_name($result,1);
$third= mysql_field_name($result,2);
if($nameIdx!=$row[$third])
$nameIdx=$row[$third];
}
if($row[$third]!=NULL){
array_pop($row);
$toJson[$selectname[$i]][$nameIdx][]=$row;
}
}
}
$inJson=json_encode($toJson);
So i get something like:
{
"select1": { "optgroup1":[{"v":"1","l":"option1"},{"v":"2","l":"option2"}],
"optgroup2":[{"v":"3","l":"option1"},{"v":"4","l":"option2"}],
"optgroup3":[{"v":"5","l":"option1"},{"v":"6","l":"option2"}]
},
"select2": { " (...)
}
//optgroup is parentCategory
//options are categories no-parents
Use something recursive: