Shane Wade Shane Wade - 24 days ago 10
JSON Question

PHP JSON value retrieval error

I have a problem to retrieve JSON value from my PHP encoded JSON file

This is my PHP code:

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

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

while ($r2 = mysql_fetch_array($result2)) {

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

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


This is the output 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. Is this JSON format correct? simply explain: I am getting 1 question for answers.



Here this is the jQuery code I am trying to getting result.

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


It will show question title and description correctly. But only show 1 answer? according to my JSON file there are 3 answers. I want to show 3 answers under the question. May I have to change my JSON format?
Thanks in advance!

Answer

Your JSON is valid but format is incorrect as it should have all answers under one node like this:

[{
    "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"
    }]
}]

To get above JSON format, please change your PHP code as following:

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

And the jQuery part:

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