2016-10-07 19 views
5

bây giờ tôi đang làm việc trên nhận được phản hồi Json từ các dữ liệu trong bảng sauJson đáp ứng Query

enter image description here

Bằng việc sử dụng Query sau:

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

tôi có thể nhận phản hồi Json là:

enter image description here

Bây giờ những gì tôi muốn là cho các đối tượng tổng thể mà không sử dụng mệnh đề where (nơi subject_name = 'toán')

tôi cần Json o/p như sau:

enter image description here

+1

Bạn có thể cần phải làm nhiều truy vấn (đầu tiên tìm thấy có bao nhiêu đối tượng có, sau đó vòng lặp mỗi chủ đề) hoặc thực hiện logic với PHP (các mục nhóm có cùng tên chủ đề). –

Trả lời

5

Bạn có thể nhóm kết quả theo subject_name vào mảng PHP bằng mã sau.

$sql = "select * from users"; 

HOẶC

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

PHP MÃ

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

Bây giờ bạn có thể nhận dạng JSON của bạn từ trên mã.

+0

Xin chào, mã của bạn cung cấp mảng như "tiếng Anh" nhưng tôi cần có khóa như "subject_name: English". Vui lòng kiểm tra yêu cầu json o/p trong câu hỏi của tôi giao phối. bạn có thể làm rõ tôi làm thế nào để có được rằng thực hiện –

+0

Ahh! Có chắc chắn! làm ơn chờ. Tôi đang cập nhật mã cho bạn! @RajaGopal –

+0

Đang chờ bạn cập nhật của bạn –

3

Bạn có thể thử mã này

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

    } 
+0

Xin chào, những gì tôi muốn không phải theo tên chủ đề trong mã. tên chủ đề có thể thay đổi trong tương lai vì vậy tôi muốn lấy theo tên chủ đề trong db không trực tiếp trong mã –

1

Mã của tôi có thể không chính xác đúng, nhưng vẫn có thể giúp bạn

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

Hãy thử:

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