I have difficulties to construct this kind of JSON object from two SQL results when nothing is returned by the second SQL query.
{"options":["American Black Bear", "Asiatic Black Bear", "Brown Bear", "Giant Panda", "Sloth Bear", "Sun Bear", "Polar Bear", "Spectacled Bear"], "selected":[]}
I want to always have the selected key and empty array even if no lines are returned from the SQL statment.
here is the PHP code in run to execute the two queries
$sql = "SELECT categorie FROM cc_categorie order by categorie";
$sth= $dbh->query($sql);
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
$rows['options'][] = $row['categorie'];
}
$sql = "SELECT cc_categorie.categorie
FROM cc_coupon_by_categorie, cc_categorie
WHERE cc_categorie.id_categorie = cc_coupon_by_categorie.id_categorie
and cc_coupon_by_categorie.id_coupon = 10"; //no lines are returned (I know I need to use join on...)
$sth= $dbh->query($sql);
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
$rows['selected'][] = $row['categorie'];
}
print json_encode($rows);