Reputation: 3225
I am using an array with multiple SQL Queries then using a foreach loop to run the queries:
foreach($sql as $query) {
$rs=mysql_query($query, $conn);
if(mysql_num_rows($rs) > 0){
while($result=mysql_fetch_assoc($rs)) {
$json_arr["id"] = $result['sequence'];
$json_arr["value"] = $result['company'];
$json_arr["label"] = $result['company'];
array_push($display_json, $json_arr);
}
} else {
$json_arr["id"] = "#";
$json_arr["value"] = "";
$json_arr["label"] = "No Result Found!";
array_push($display_json, $json_arr);
}
}
i then need to use json_encode
and print the results like:
$jsonWrite = json_encode($display_json); //encode that search data
print $jsonWrite;
how can i do one json_encode
for all the query results?
Upvotes: 0
Views: 360
Reputation: 4738
foreach($sql as $query) {
$rs=mysql_query($query, $conn);
if(mysql_num_rows($rs) > 0){
while($result=mysql_fetch_assoc($rs)) {
$json_arr["id"] = $result['sequence'];
$json_arr["value"] = $result['company'];
$json_arr["label"] = $result['company'];
}
array_push($display_json, $json_arr);
} else {
$json_arr["id"] = "#";
$json_arr["value"] = "";
$json_arr["label"] = "No Result Found!";
array_push($display_json, $json_arr);
}
}
Moving the first array_push to outside of the while loop, as above, will prevent the repetition you're seeing.
Upvotes: 1