1. You have one table and two column.
  2. One column contain repeated data and second column contain different different  repeated data.
  3. Task is to write second column data as JSON array and First column data as JSON object.

Database Table:

Database

Output Like.

Output

Solution:

<?php
$con = mysql_connect(“localhost”,”root”,””);
if(!$con)
{
die(“Could Not connect”.mysql_error());
}
mysql_select_db(“virtual_school_app”,$con);

$tea_id=”15″;
$output=array();

$result=mysql_query(“SELECT DISTINCT `standard`,`division` FROM `timetable_teacher_detail` WHERE `Monday`=$tea_id”);
while ($row = mysql_fetch_assoc($result)) {
$key = $row[‘standard’];
$output[$key][] = $row[‘division’];
}
echo json_encode($output);
mysql_close($con);
?>

 

 

Advertisements