5

I am now working on getting a Json response from the data in the following table

enter image description here

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 :

enter image description here

Now what i want was for overall subjects without using where clause (where subject_name = 'maths')

My required Json o/p is as below :

enter image description here

1
  • 1
    You probably need to do multiple queries (first find how many subjects there are, then loop each subject) or do logic with PHP (group items with same subject name). Commented Oct 7, 2016 at 5:44

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello , your code gives array like "English " but i need with key like "subject_name : English". Kindly check the required json o/p in my question mate . can you clarify me how to get that done
Ahh! Yes Sure! wait please. I am updating code for you! @RajaGopal
There was a little bit mistake. I updated again. Now fixed. Check please. @RajaGopal
Thanks a lot sumon , Thanks for your hard work on this question , marked this answer as working :-)
3

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']);
                    }

    }

1 Comment

Hello , what i want was not by subject name in the code. subject name might change in the future so i want to take by subject name in the db not directly in the code
3

Try:

$sql = " select subject_name, group_concat(unit) unit, group_concat(unit_name) unit_name
          from subject group by subject_name ";

$result = mysqli_query($conn, $sql);

$data = [];
while($row = mysqli_fetch_array($result))
{
    $subject_name = $row['subject_name'];
    $units = explode( ',', $row['unit'] );
    $unit_names = explode( ',', $row['unit_name'] );

    $subject = [];
    $subject['subject_name'] = $subject_name;

    $emparray = [];
    for ($i=0; $i < count($units); $i++) {
        $emparray[] = array('unit' => $units[$i], 'unit_name' => $unit_names[$i]);
    }

    $subject['units'] = $emparray;
    $data[] = $subject;
}

echo json_encode($data);

Comments

1

My code may not be exactly correct, but may still help you

$outer_sql = "SELECT DISTINCT subject_name * from subject;
  $outer_result =  mysqli_query($conn,$outer_sql);
  while($outer_query_row= mysqli_fetch_array($outer_result))
  {
 $sql = "select * from subject where subject_name = '".$outer_query_row['subject_name']."'";

 $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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.