0
Good night,
I am trying to pass an array to json but it only passes the last variable
function GetSuppliersView() {
global $db;
global $id;
global $type;
try{
$query = $db->query("SELECT * FROM suppliers WHERE id = $id");
$row=$query->fetch(PDO::FETCH_ASSOC);
$result['success'] = true;
$result['result'] = $row;
$querytwo = $db->query("SELECT name FROM third_party_services LEFT JOIN suppliers_services ON third_party_services.id = suppliers_services.id WHERE supplier_id=$id");
$x=0;
while($services = $querytwo->fetch(PDO::FETCH_ASSOC)) {
$var[$x] = $services['name'];
}
$result['secondresult'] = array_values($var);
echo json_encode($result);
return true;
} catch (PDOException $pe) {
return false;
}
}
The variable $result['result'] passes right, but the $result['secondresult'] only passes the last item to be assigned
Forehead to wear
$row=$query->fetch_assoc(PDO::FETCH_ASSOC);
so you have an array that you can export withjson_encode
andecho
.– Sergio
The
$x
is always0
, does not increase, it is expected that only the last.– Inkeliz