Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json Response Query

Tags:

json

php

mysql

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

like image 863
Raja Gopal Avatar asked Oct 07 '16 05:10

Raja Gopal


People also ask

What is response JSON in Python?

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.

What is @JSON_query in jQuery?

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.

How does the JSON () method of the response interface work?

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.

Why can't I point to the source of a JSON request?

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).


2 Answers

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.

like image 152
Sumon Sarker Avatar answered Sep 23 '22 04:09

Sumon Sarker


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

    }
like image 29
Kiran Dsouza Avatar answered Sep 24 '22 04:09

Kiran Dsouza