2013-03-16 97 views
0

我正在用PHP和Javascript创建一个多选题测验游戏。问题可以有2到6个答案。我一直在下面的代码中使用PHP查询来获取测验问题的数组,以及每个问题的答案。将多个PHP数组转换为一个数组,准备转换为Javascript

我现在想在Javascript中使用这些数据,并希望每次用户单击下一个问题时都会显示一个问题和相应的答案。

我认为(但我不确定)最好的方法是将数组合并成一个问题数组包含答案数组之前转换为一个JavaScript数组。

这真的是我应该这样做的方式,如果是这样,我该怎么做?

$thisquizid = $_POST['quizidvalue']; 
    for ($j = 0; $j < $questionrows; ++$j) 
     { 
      $questionresult = pg_fetch_array($questionquery); 
      $answerquery = pg_query($db_handle, "SELECT * FROM answer WHERE questionid = '$questionresult[0]'"); 
      $answerrows = pg_num_rows($answerquery); 

     for ($i = 0; $i < $answerrows; ++$i) 
      { 
       $answerresult = pg_fetch_array($answerquery); 
      } 

    } 
+1

我建议你在[Code Review](http://codereview.stackexchange.com/)上提问这个问题。 – 2013-03-16 22:38:12

回答

0

让我们从另一个方向来看这个。 JS中的数据应该是什么样子?或者说,PHP应该返回什么样的JSON才能让JS变得更好,更干净。例如:

var json = { // From PHP via AJAX 
    "questions_and_answers": [ 
    { 
     "question": "What is the airspeed velocity of an unladen swallow?", 
     "answers": [ 
     "11 meters per second", 
     "24 miles an hour", 
     "African or European?" 
     ] 
    }, 
    { 
     "question": "If a tree falls in an empty forest does it make a sound?", 
     "answers": [ 
     "Unequivocally, yes.", 
     "Unequivocally, no.", 
     "It's all subjective." 
     ] 
    } 
    ] 
}; 

for(var i = 0, j = json.questions_and_answers.length; i < j; i++) { 
    var question = json.questions_and_answers[i].question; 
    var answers = json.questions_and_answers[i].answers.join("\n"); 
    alert(question + "\n" + answers); 
} 

现在,在PHP中看起来如何?最简单的事情通过json_encode,因为你没有使用答问物体或某种ORM的,将被嵌套数组:

<?php 
$questions_and_answers = array(
    array(
     "question" => "What is the airspeed velocity of an unladen swallow?", 
     "answers" => array(
      "11 meters per second", 
      "24 miles an hour", 
      "African or European?" 
     ) 
    ), 
    array(
     "question" => "If a tree falls in an empty forest does it make a sound?", 
     "answers" => array(
       "Unequivocally, yes.", 
       "Unequivocally, no.", 
       "It's all subjective." 
      ) 
    ) 
); 

// Wrap in a parent object, assuming you're delivering to JS with via AJAX. 
echo json_encode(array("questions_and_answers" => $questions_and_answers)); 
?> 

那么你是在正确的轨道与你的服务器端代码上,但你需要一个空数组来填充数据库结果:

<?php 

$thisquizid = $_POST['quizidvalue']; 

    $json_output = array(); 

    for ($j = 0; $j < $questionrows; ++$j) 
     { 
      $questionresult = pg_fetch_array($questionquery); 
      $answerquery = pg_query($db_handle, "SELECT * FROM answer WHERE questionid = '$questionresult[0]'"); 
      $answerrows = pg_num_rows($answerquery);    
      $json_output[$j] = array("question" => $questionresult["text"], "answers" => array()); 

     for ($i = 0; $i < $answerrows; ++$i) 
      { 
       $answerresult = pg_fetch_array($answerquery); 
       $json_output[$j]["answers"][] = $answerresult["text"]; 
      } 

    } 

    echo json_encode(array("questions_and_answers" => $json_output)); 

?> 
+0

谢谢,我打算先给这个解决方案一个镜头。 – user2177629 2013-03-16 23:40:25

+0

祝你好运!只是修正了最后的代码块,请忽略以前的草率版本。使用包含问题/答案文本的实际列名替换'$ questionresult [“text”]'和'$ answerresult [“text”]'中的'text'。 – 2013-03-17 00:00:07

+0

非常感谢,我设法让这个工作,你的帮助是非常有用的谢谢! – user2177629 2013-03-17 12:19:30

0

当然,你可以做这样假设你不就是想在Javascript(IE一切实际上是人们会想欺骗,所以你需要的答案存储在服务器上测验.. )。

使用AJAX调用你的PHP页面,然后只返回你需要显示为一个变量,JSON数组,无论...基本AJAX调用看起来像这样的瓦莱斯:

var xmlhttpMain=new XMLHttpRequest(); 
function changeContent(){ 
    xmlhttpMain.open("GET", true); 
    xmlhttpMain.send(null); 
    xmlhttpMain.onreadystatechange=function(){ 
    if (xmlhttpMain.readyState==4 && xmlhttpMain.status==200){ 
      newStuff=xmlhttpMain.responseText;//This is your JSON or Array that you create in the PHP file... 
      //Then run whatever code you want here 
    } 
} 

在你的PHP文件只是使用“echo($ someString);”把你的信息拍回JS地...

+0

谢谢你的帮助:) – user2177629 2013-03-16 23:40:41