I am now working on getting a Json response from the data in the following table
By using the following Query :
$sql = "select * from subject where subject_name = 'maths'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
$data = new stdClass();
$subject_name = $row['subject_name'];
$unit = $row['unit'];
$unit_name = $row['unit_name'];
$data->subject_name=$subject_name;
$emparray[] = array('unit' => $unit,
'unit_name' => $unit_name,
);
$data->units=$emparray;
}
echo json_encode(array($data));
I can get the Json response as :
Now what i want was for overall subjects without using where clause (where subject_name = 'maths')
My required Json o/p is as below :
response.json () returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.
json_query is using the jmespath Query language. It is a powerful query language to parse JSON content. It helps you to parse JSON content and filter the elements you want. You can pretty much do everything with JSON It has a nice toolset as well, where you could write and test your queries before you try it in realtime.
The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON. Note that despite the method being named json(), the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object.
Pointing to "/data" would be invalid because the request document did not have a value at "/data", and source is always given with reference to the request document.) If the server cannot parse the request as valid JSON, including source doesn’t make sense (because there’s no JSON document for source to refer to).
You can group result by subject_name to your PHP array by following code.
$sql = "select * from users";
OR
$sql = "select subject_name,unit,unit_name from users";
PHP CODE
$result = mysqli_query($conn,$sql);
$data = array();
function search($subject){
global $data;
foreach ($data as $key => $value) {
if (isset($value['subject_name']) && $value['subject_name']==$subject) {
return $key;
}
}
return false;
}
while($row = mysqli_fetch_assoc($result)){
$res = search($row['subject_name']);
if ($res===false) {
array_push($data, array(
'subject_name' =>$row['subject_name'],
'units'=>array($row)
)
);
}else{
array_push($data[$res]['units'], $row);
}
}
echo json_encode($data);
Now you can get your JSON format from above code.
You can try this code
$sql = "select * from subject;
$result = mysqli_query($conn,$sql);
while($query_row= mysqli_fetch_array($result))
{
if($query_row['subject_name'] == 'English'){
$timeslot = array(
"unit" => $query_row['unit'],
"unit_name" => $query_row['unit_name']
);
$subjects = addToArray($subjects, $timeslot['subject_name']);
} else if($query_row['subject_name'] == 'Maths'){
$timeslot = array(
"unit" => $query_row['unit'],
"unit_name" => $query_row['unit_name']
);
$subjects = addToArray($subjects, $timeslot['subject_name']);
} else if($query_row['subject_name'] == 'Science'){
$timeslot = array(
"unit" => $query_row['unit'],
"unit_name" => $query_row['unit_name']
);
$subjects = addToArray($subjects, $timeslot['subject_name']);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With