0

我正在调用 PHP 脚本来获取结果,然后将数组转换为 javascript 数组。我的数组是用于多项选择测验,可以有 2 到 6 个答案,它用答案拉出问题。

我一直在做很多测试,有时会在预期的时候丢失记录。例如,在拉下一个应该有 5 个答案的问题后,只拉出 4 个答案。即使我重新加载测验,也会丢失相同的结果。

这似乎是随机的,没有关于何时出现此问题的模式,例如,第一次测试发现第一个有六个答案的问题缺少一个答案结果。另一个测试发现第三个问题是一个四个答案的问题,缺少一个答案结果。

我使用了 Javascript 控制台,当其中一个问题缺少结果时,我可以识别的唯一模式是 answerid 始终是第一个,即如果 answerid 是 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
4

1 回答 1

0

您丢失的行来自 PHP 中的逻辑错误。

在示例的第 4 行中$questionrows = pg_fetch_array($questionquery);,您正在调用 pg_num_rows()。这将获取第一行,然后您将忽略该行,因为while循环的测试条件while($row = pg_fetch_array($questionquery))再次获取数据集的下一行。

由于在循环之前似乎不需要任何行内容,我建议评论或删除第 4 行。

于 2013-03-18T14:25:28.600 回答