2013-03-16 67 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

相關問題