I need to build a JSON file from PHP with a specific sequence, but I do not know how to build it.
The sequence is as follows:
[{"jornada":"A","colegios":["Salle","Belemitas"]},
{"jornada":"B","colegios":["Nazaret","Comfama","jega"]}]
the database is as follows:
id | colegio | jornada |
-----------------------------
1 | Salle | A |
-----------------------------
2 | Nazaret | B |
-----------------------------
3 | Comfama | B |
-----------------------------
4 | Belemitas | A |
-----------------------------
5 | Jega | B |
-----------------------------
6 | Rosario | Unica |
-----------------------------
My attempt at PHP
$con = mysqli_connect($server, $user, $password, $database);
if (mysqli_connect_errno()) {
echo "APP: " . mysqli_connect_error();
}
$query = mysqli_query($con, "SELECT * FROM ge_colegios ORDER BY jornada DESC; ");
$json = '[';
while ($row = mysqli_fetch_array($query)){
$char ='"';
$json .=
'{
COMO CONSTRUYO LE SECUENCIA AQUI?
},';
}
// buat menghilangkan koma diakhir array
$json = substr($json,0,strlen($json)-1);
$json .= ']';
echo $json;
mysqli_close($con);
?>
Thanks