php - Need Json in the given form -
i want json in given form web sercie:
"header":{ "code":"0", "message":"success" }, "body":{ "questions":{ "1":"who microsoft ceo", "2":"oop stands for?" }, "answers":[ "question 1":[ "bill gates", "nelson mandela", "nsteve jobs", "robert andrew",] "question 2":[ "object oriented program", "object oriented programing", "object oriented proffesion", "object oriented prefix" ] } }
i getting
"header":{ "code":"0", "message":"success" }, "body":{ "questions":{ "1":"who microsoft ceo", "2":"oop stands for?" }, "answers":[ "bill gates", "nelson mandela", "nsteve jobs", "robert andrew", "object oriented program", "object oriented programing", "object oriented proffesion", "object oriented prefix" ] } }
the code "questionandanswer.php" ,every thing runs fine need code nessted loop part got required json
<?php /** * created phpstorm. * user: user * date: 6/9/14 * time: 3:47 pm */ require('../lib.php'); global $db; $json = array(); $json['header']['code'] = '0'; $json['header']['message'] = 'success'; $json['body'] = ''; $query_1 = "select * `questions`"; $getnumofrows = $db->executequery($query_1); $res_id = $getnumofrows->reshandle; $numberofrows = mysql_num_rows($res_id); $count_ques = 0; $count_ans = 0; while ($getnumofrows_fromdb = $getnumofrows->fetchasarray()) { $question = $getnumofrows_fromdb['question_descrip']; $json['body']['questions'][$getnumofrows_fromdb['id']] = $question; $query_ans = "select * `answers` question_id=".$getnumofrows_fromdb['id'].";"; $getnumofrows_ans = $db->executequery($query_ans); while ($getnumofrows_fromdb_ans = $getnumofrows_ans->fetchasarray()) { $answers[$count_ans] = $getnumofrows_fromdb_ans['answer_opt']; //$ques_id= $getnumofrows_fromdb_ans['answer_opt']; $json['body']['answers'] = $answers; $count_ans++; } $count_ques++; } echo json_encode($json); ?>
please tell me code can desire json. thanx
Comments
Post a Comment