<?php
mysql_pconnect("localhost","root","");
mysql_select_db("AppDevelopMenu") or die( '1');
$sql1=mysql_query("select Question from questions where QId ='1'");

if(!$sql1)
echo '2';

else{

$question=mysql_fetch_assoc($sql1) ;
$outputq[]=$question;


$sql=mysql_query("select OptionId,OptionContent from options where QId ='1'");

if(!$sql)
{
echo '3';
}

else {
while($row=mysql_fetch_assoc($sql))
$outputq[]=$row;
print(json_encode($outputq));
}
}

mysql_close();

?>

i have to modify this code to read json in a different structure that lets say question is the root node..QuestionContent and options are there on next level.And then options is the root node for option1,option2,option3 etc
How should I modify my php code to achieve this

Member Avatar for diafol

Not sure if this is what you want:

<?php
$qid = 1;
$sql1=mysql_query("select Question from questions where QId = $qid");
if(!$sql1){
    echo '2';
}else{
    $question=mysql_fetch_assoc($sql1) ;
    $outputq['question'] =$question['Question'];
    $sql=mysql_query("select OptionId,OptionContent from options where QId = $qid");
    if(!$sql){
        echo '3';
    }else{
        while($row=mysql_fetch_assoc($sql)){
            $outputq['options'][] = array('id'=>$row['OptionId'], 'content'=>$row['OptionContent']);
        }
    }
    print(json_encode($outputq));
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.