2016-11-16 11 views
1

私はJSONファイルエンコードされた私のPHPからJSON値を取得するために問題を抱えているPHP JSONは、値の取得エラー

これは私のPHPコードです出力JSON

[{ 
    "qid": "1", 
    "q_title": "This is first question title", 
    "q_question": "This is first question description", 
    "Ans": { 
     "aid": "26", 
     "answer": "This is first answer" 
    } 
}, { 
    "Ans": { 
     "aid": "27", 
     "answer": "This is second answer" 
    } 
}, { 
    "Ans": { 
     "aid": "28", 
     "answer": "This is third" 
    } 
}] 
  1. このJSON形式は正しいですか?単純に説明する:私は答えのための1つの質問を得ている。

これは結果を得ようとしているjQueryコードです。

$(document).ready(function() { 
    $.ajax({ 
     type: "POST", 
     cache: false, 
     dataType:"json", 
     url: 'data.php', 
     success: function(data){ 
      $('.show_divis').each(function (index, value){ 
       var data_votes = ''; 

       data_votes += '<div style="color:#000">'+data[index].q_title+'</div>'; 
       data_votes += '<div style="color:#555">'+data[index].q_question+'</div>'; 

       $(this).html(data_votes).fadeOut(300).fadeIn(400); 

       $('.show_divis2').each(function (index, value){ 
        var data_votes2 = ''; 

        data_votes2 += '<div style="color:#000">'+data[index].Ans.aid+'</div>'; 
        data_votes2 += '<div style="color:#555">'+data[index].Ans.answer+'</div>'; 

        $(this).html(data_votes2).fadeOut(300).fadeIn(400); 
       }); 
      }); 
     } 
    }); 
}); 

質問タイトルと説明が正しく表示されます。しかし、答えは1つのみ表示されますか?私のJSONファイルによれば、3つの答えがあります。私はその質問の下に3つの答えを示したい。 JSON形式を変更する必要がありますか? ありがとうございます!

+1

使用しているフォーマットは間違いなくトップレベルの編曲での位置として便利ではありませんayは、回答が属する質問を決定します。すべての回答を、それらが所属する質問の中の配列にまとめるほうが論理的です。その後、PHPですでにやっているように、2つの入れ子になったループをjavascriptで使うことができます。 – jeroen

+0

こんにちは、返信いただきありがとうございます。 HTML部分にのみ、あなたは私があまりにも... –

答えて

2

あなたのJSONが有効であるが、それはこのような一つのノードの下にすべての答えが必要とフォーマットが正しくありません:JSON形式の上に取得するには

[{ 
    "qid": "1", 
    "q_title": "This is first question title", 
    "q_question": "This is first question description", 
    "Ans": [{ 
     "aid": "26", 
     "answer": "This is first answer" 
    }, 
    { 
     "aid": "27", 
     "answer": "This is second answer" 
    }, 
    { 
     "aid": "28", 
     "answer": "This is third" 
    }] 
}] 

を、以下のようにPHPコードを変更してください:

$i = 0; 
$qinfo = array(); 
$qry = "SELECT qid, q_title, q_question FROM questions WHERE qid = 1"; 
$result = mysql_query($qry); 

while ($r = mysql_fetch_array($result)) { 

    $qinfo[$i]['qid'] = $r['qid']; 
    $qinfo[$i]['q_title'] = $r['q_title']; 
    $qinfo[$i]['q_question'] = $r['q_question']; 

    $qry2 = "SELECT aid, answer FROM answers WHERE qid=".$r['qid']." "; 
    $result2 = mysql_query($qry2); 

    $j = 0; 
    while ($r2 = mysql_fetch_array($result2)) { 

     $qinfo[$i]["Ans"][$j]["aid"] = $r2['aid']; 
     $qinfo[$i]["Ans"][$j]["answer"] = $r2['answer']; 

     $j++; 
    } 
    $i++; 
} 
echo json_encode($qinfo); 

とjQueryの一部:

$(document).ready(function() { 
    $.ajax({ 
     type: "POST", 
     cache: false, 
     dataType:"json", 
     url: 'data.php', 
     success: function(data){ 
      var data_votes = ''; 

      $.each(data, function (index, questions){ 
       //console.log(questions); 
       data_votes += '<div style="display: block; background-color: #eee; margin: 5px 0px; padding: 5px;">'; 
       data_votes += '<h2 style="padding: 5px; margin: 0px;">'+questions.q_title+'</h2>'; 
       data_votes += '<p style="padding: 5px;">'+questions.q_question+'</p>'; 

       $.each(questions.Ans, function (index, answers){ 
        //console.log(answers); 
        data_votes += '<div style="color:#555; padding: 5px; margin: 2px 0px; background-color: #ccc;" id="answer_'+answers.aid+'">'+answers.answer+'</div>'; 
       }); 
       data_votes += '</div>'; 
      }); 
      // Add your reference to your desired html element instead of "BODY" 
      $('body').append(data_votes); 
     } 
    }); 
}); 
+0

グレートそのクラスで2つのdiv要素を持っています'showdivis'と' showdivis2'の違いは?これらのHTMLはあなたに質問できますか?私はAJAXから返されたデータを反復処理するのではなく、 '.each()'でこれらのdivを繰り返し処理している理由を理解することができます。 –

+0

のために、私はすでにこの回答に投票していますあまりにも jQueryの一部と少しの助けを喜ばせることができ、

+0

は構造は何であることを追加してみましょう –