2013-03-18 21 views
0

我正在調用一個PHP腳本來獲取結果,然後將數組轉換爲一個javascript數組。我的數組是用於多項選擇測驗,可以有2到6個答案,它將問題與答案拉近。偶爾從PHP/JSON數組中缺少一條記錄

我一直在做很多測試,有時一個記錄缺失時,它是預期的。例如,拉動下一個應該有5個答案的問題後,只有4個答案被拉。即使我重新加載測驗,也會丟失相同的結果。

這看起來是隨機的,沒有發生這種問題的模式,例如,第一次測試發現有六個答案的第一個問題缺少答案結果。另一項測試發現,第四個答案的問題是缺少答案。

我使用了Javascript控制檯,以及當這些問題之一缺少結果時我可以識別的唯一模式,它始終是第一個答案,即如果答案是285,286,287,那麼285將是失蹤者。

在25個測試問題中,只有3個結果缺失。

這裏是我的代碼,PHP腳本...

$thisquizid = $_REQUEST['quizidvalue']; 
$questionquery = pg_query($db_handle, "SELECT * FROM question LEFT JOIN answer USING (questionid) WHERE quizid = '$thisquizid'"); 
$questionrows = pg_num_rows($questionquery); 
$questionresult = pg_fetch_array($questionquery); 

$questions = array(); 

while($row = pg_fetch_array($questionquery)) { 
    $questions[$row['questionid']][] = $row; 
} 
die(json_encode($questions)); 

而且我的JavaScript

var questions; 
var currentquestion = 0; 


$(document).ready(function(){ 

     console.debug("in ajax"); 
     jQuery.ajax({ 

        error: function (data, textStatus, errorThrown){ 
         console.log(data); 
         console.log(textStatus); 
         console.log(errorThrown); 
        }, 
        url:"quizajax.php", 
        dataType: "json", 
        data: { 
         quizidvalue: <?=$thisquizid?> 
        }, 


      }).done(function(data) { 
        questions = data; 
        for(i in data){ 
         console.log(data[i]); 

        } 
       }); 


     $("#nextquestionbtn").click(function() { 
      nextQuestion(); 
     }); 
    }); 

function nextQuestion(){ 
    for(i in questions) { 
     if(i<=currentquestion) 
      continue; 

     currentquestion = i; 


     for(y in questions[i]) { 
      console.log("CurrentA: "+ currentquestion); 
      console.log("I: " + i); 
      console.log(questions[i][y].answerid); 
     } 


     console.log("CurrentQ: "+ currentquestion); 
     console.log("I: " + i); 
     console.log(questions[i]); 

     questionVariables(); 


     break; 
    } 

這裏是它的一個問題是在我的數據庫記錄

questionid | quizid | questiontype | qdescription | qfilelocation | noofanswers | answertype | answerid | adescription | afilelocation | iscorrect 
------------+--------+--------------+--------------+---------------+-------------+------------+----------+--------------+---------------+----------- 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  795 | sadfds  | null   | t 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  796 | asdfsd  | null   | f 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  797 | asfsadf  | null   | f 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  798 | asdfsadf  | null   | f 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  799 | asdfsadf  | null   | f 
     295 |  57 | text   | 6mark work | null   |   6 | text  |  800 | sadfasdf  | null   | f 

回答

0

您缺少的行來自PHP中的邏輯錯誤。

在示例$questionrows = pg_fetch_array($questionquery);的第4行中,您正在調用pg_num_rows()。這會提取第一行,然後由於while循環while($row = pg_fetch_array($questionquery))的測試條件而忽略,然後再次獲取數據集的下一行。

因爲它似乎沒有必要對任何行的內容之前的循環,我建議發表評論,或刪除,行4

+0

http://www.php.net/manual/en/function .pg-num-rows.php - 我沒有看到有關在pg_num_rows上提取數據的信息。 – ZigZag 2013-03-18 14:28:17

+0

更正:他在第4行調用pg_fetch_array,而不是pg_num_rows。 – 2013-03-18 14:29:05

+0

評論說,你是對的,這是不必要的,但問題仍然存在。 – user2177629 2013-03-18 14:31:39