Json响应查询

时间:2016-10-07 05:22:34

标签: php mysql json

我现在正致力于从下表中的数据中获取Json响应

enter image description here

使用以下查询:

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

我可以将Json的回复视为:

enter image description here

现在我想要的是整体科目而不使用where子句(其中subject_name ='数学')

我需要的Json o / p如下:

enter image description here

4 个答案:

答案 0 :(得分:4)

您可以通过以下代码将 subject_name 的结果分组到 PHP数组

$sql    = "select * from users";

$sql    = "select subject_name,unit,unit_name from users";

PHP代码

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

现在,您可以从上面的代码中获取 JSON 格式。

答案 1 :(得分:3)

您可以尝试此代码

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

    }

答案 2 :(得分:3)

尝试:

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

答案 3 :(得分:1)

我的代码可能不完全正确,但可能仍然可以帮助您

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