2013-03-18 2 views
0

PHP 스크립트를 호출하여 결과를 얻은 다음 배열을 자바 스크립트 배열로 변환합니다. 내 배열은 객관식 퀴즈에 대한 것입니다, 거기에 2 ~ 6 답변, 그것은 대답과 함께 질문을 당길 수 있습니다.때때로 PHP/JSON 배열에 레코드가 없습니다.

나는 많은 테스트를 해왔으며 예상되는 경우 레코드가 누락되는 경우가 있습니다. 예를 들어, 5 개의 답을 가져야하는 다음 질문을 당긴 후 4 개의 답 만 가져옵니다. 퀴즈를 새로 고침해도 동일한 결과가 누락됩니다.

이것은 임의적입니다.이 문제가 발생할 때 패턴이 없습니다. 예를 들어 첫 번째 테스트에서 여섯 개의 대답이있는 첫 번째 질문에서 대답 결과가 누락되었습니다. 또 다른 테스트에서는 네 번째 질문 인 세 번째 질문을 발견했습니다. 네 가지 질문은 대답 결과가 누락되었습니다.

자바 스크립트 콘솔을 사용했는데이 질문 중 하나에 결과가 누락되었을 때 확인할 수있는 유일한 패턴은 항상 첫 번째가 될 answerid입니다. 즉, answerids가 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)); 

그리고 내 자바 스크립트

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의 논리 오류에서 발생합니다.

예제의 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

+0

수정 : 그는 pg_num_rows가 아니라 4 행에서 pg_fetch_array를 호출합니다. –

+0

댓글을 달았습니다. 문제가 해결되지 않았지만 문제가 해결되었습니다. – user2177629

관련 문제