I'm trying to create an array of objects with data coming from multiple tables, lets say there is table that hold patient data and there is table that hold diagnosis, and medicine for every patient that admitted to clinic, and i need to create an array of objects with the following output. Screen shot
And i have to write the following code
<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'new_nhif');
define('USERNAME', 'root');
define('PASSWORD', '');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE, PORT);
$sql = sprintf(
'SELECT
nh.MembershipNo,
nh.FullName,
nh.id as nhid,
lb.labrequest,
fd.diagnosis,
fd.DiseaseCode,
fd.CreatedBy as fdcrb,
dz.name
FROM nhif_data AS nh
LEFT JOIN laboratory AS lb ON lb.re_id = nh.id
LEFT JOIN foliodisease AS fd ON fd.re_id = nh.id
LEFT JOIN dawa_zilizotoka AS dz ON dz.re_id = nh.id
WHERE lb.re_id = nh.id
AND fd.re_id = nh.id
AND dz.re_id = nh.id
-- GROUP BY nh.MembershipNo
'
);
$obj = new stdClass;
$result = $connection->query($sql);
$vipimo = array();
$dawa = array();
$all = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// print_r(json_encode(['entities'=> $row],JSON_PRETTY_PRINT));
$obj->MembershipNo = $row['MembershipNo'];
$obj->FullName = $row['FullName'];
$id = $row['nhid'];
$sql2 = "SELECT * FROM foliodisease WHERE re_id ='$id'";
$result1 = $connection->query($sql2);
if ($result1->num_rows > 0) {
while($row2 = $result1->fetch_assoc()) {
$vipimo['diagnosis']= $row2['diagnosis'];
$vipimo['DiseaseCode']= $row2['DiseaseCode'];
$obj->FolioDiseases[] = $vipimo;
}
}
$sql3 = "SELECT * FROM dawa_zilizotoka WHERE re_id = $id";
$result3 = $connection->query($sql3);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$dawa['name']= $row3['name'];
$obj->FolioItems[] = $dawa;
}
}
$all[] = $obj;
}
print_r(json_encode(['entities'=> $all], JSON_PRETTY_PRINT));
}
?>
And it give out the following output
{
"entities": [
{
"MembershipNo": "602124502",
"FullName": "Omari M Simba",
"FolioDiseases": [
{
"diagnosis": "typhoid",
"DiseaseCode": "J54"
},
{
"diagnosis": "homa",
"DiseaseCode": "L54"
},
{
"diagnosis": "malaria",
"DiseaseCode": "b54"
}
],
"FolioItems": [
{
"name": " Fluticasone furoate\t"
},
{
"name": " Acyclovir Eye ointment\t"
},
{
"name": " Acyclovir\t"
},
{
"name": " Acyclovir\t"
}
]
},
{
"MembershipNo": "602124502",
"FullName": "Omari M Simba",
"FolioDiseases": [
{
"diagnosis": "typhoid",
"DiseaseCode": "J54"
},
{
"diagnosis": "homa",
"DiseaseCode": "L54"
},
{
"diagnosis": "malaria",
"DiseaseCode": "b54"
}
],
"FolioItems": [
{
"name": " Fluticasone furoate\t"
},
{
"name": " Acyclovir Eye ointment\t"
},
{
"name": " Acyclovir\t"
},
{
"name": " Acyclovir\t"
}
]
}
]
}
My tables are
nhif_data ---- nhif_data ,
laboratory ---- laboratory,
foliodisease --- foliodisease ,
dawa_zilizotoka ---- dawa_zilizotoka
whileloop (at the beginning) so that a new instance is created in each iteration.sprintf()if you aren't using any placeholders? That is an entirely useless call. Iterated queries are a no-no.