我正在调用一个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
http://www.php.net/manual/en/function .pg-num-rows.php - 我没有看到有关在pg_num_rows上提取数据的信息。 – ZigZag 2013-03-18 14:28:17
更正:他在第4行调用pg_fetch_array,而不是pg_num_rows。 – 2013-03-18 14:29:05
评论说,你是对的,这是不必要的,但问题仍然存在。 – user2177629 2013-03-18 14:31:39