2017-10-05 9 views
1

私のデータベースには3つのテーブル(カテゴリ、catgory_details、questions)があります。私はこのようなJSONレスポンスがしたい:JSONのネストされた配列の応答は、Mysqlテーブルの最後の行のみを返します

[ 
    { 
    "category": "Accountant", 
    "deatils": { 
     "video_link": "https://www.youtube.com/", 
     "form_link": "https://docs.google.com/forms/u/0/", 
     "questions": [ 
     "Who is your idiol", 
     "What is ur name?" 
     ] 
    } 
    }, 
    { 
    "category": "Actuary", 
    "deatils": { 
     "video_link": "https://www.youtube.com/", 
     "form_link": "https://docs.google.com/forms/u/0/", 
     "questions": [ 
     "What is great?", 
     "What is ur name?" 

     ] 
    } 
    } 
] 

を私のコードは次のように質問表から1行しか返している:以下の

[ 
    { 
    "category": "Accountant", 
    "deatils": { 
     "video_link": "https://www.youtube.com/", 
     "form_link": "https://docs.google.com/forms/u/0/", 
     "questions": [ 
     "Who is your idiol" 
     ] 
    } 
    }, 
    { 
    "category": "Actuary", 
    "deatils": { 
     "video_link": "https://www.youtube.com/", 
     "form_link": "https://docs.google.com/forms/u/0/", 
     "questions": [ 
     "What is great?" 
     ] 
    } 
    } 
] 

は私のPHPコードです:

<?php 
header("Content-Type: application/json"); 
include('db.php');  
    $result = mysqli_query($conn,"SELECT * FROM categories ORDER BY id ASC"); 
    $json_response = array(); 
    while ($row = mysqli_fetch_array($result)) 
    { 
     $row_array = array(); 
     $row_array['category'] = $row['category']; 
     $id = $row['id']; 

     $detail_query = mysqli_query($conn,"SELECT * FROM category_details WHERE category_id=$id"); 
     $question_query = mysqli_query($conn,"SELECT * FROM questions WHERE category_id=$id"); 
     if($question_query->num_rows !== 0){ 
     while ($detail_fetch = mysqli_fetch_array($detail_query)) 
     { 

     while ($question_fetch = mysqli_fetch_array($question_query)) 
     { 
      $row_array['deatils'] = array(
       'video_link' => $detail_fetch['video_link'], 
       'form_link' => $detail_fetch['form_link'], 
       'questions' => [$question_fetch['question'][1],$question_fetch['question'][2]], 


       ); 


     } 
    } 

    } 
    else{ 
     while ($detail_fetch = mysqli_fetch_array($detail_query)) 
     { 

      $myid = $detail_fetch['id']; 
      $row_array['deatils'] = array(
       'video_link' => $detail_fetch['video_link'], 
       'form_link' => $detail_fetch['form_link'], 
       ); 

     } 
    } 
    array_push($json_response, $row_array); 
    } 
    echo json_encode($json_response); 

?> 

必要なJSONレスポンスを得るためにどのような変更を行う必要がありますか?
'questions' => [$question_fetch['question'][1],$question_fetch['question'][2]],

'questions' => $question_fetch['question'],

をですから、レスポンスに含まれる質問の完全な配列を持つことになります変更する

+0

question_fetchループ内だけquestionsサブアレイを移入します'$ row_array ['deatils'] = ...'あなたは '$ row_array ['deatils'] [] = ...'のようなものが必要です。 – jeroen

答えて

3

代わりquestion_fetchループ内$row_array['deatils']を構築するのには、detail_fetchループ内でそれを行う必要があり、その後、どこでもどこ、あなたがあなた自身の値を上書きしている

while ($detail_fetch = mysqli_fetch_array($detail_query)) 
{ 
    $row_array['deatils'] = array(
     'video_link' => $detail_fetch['video_link'], 
     'form_link' => $detail_fetch['form_link'], 
     'questions' => array(), 
    ); 

    while ($question_fetch = mysqli_fetch_array($question_query)) 
    { 
     $row_array['deatils']['questions'][] = $question_fetch['question']; 

    } 
} 
+0

はい、それは私のためにうまくいきました感謝、評判が低いために投票できません –

1

してみてください。

関連する問題