2013-02-05 57 views
-1

只是有一个小问题与我的验证。我想要发生的是我想通过在底部显示验证消息来检查所有行是否先添加,说明我还有很多问题。只有当所有问题都被添加并且验证通过后,它才会显示每个问题的验证,例如请选择至少一个答案,或者您选择的答案少于所需的数量。如何订购验证检查正确

目前正在做相反的事情,例如它说明我需要选择至少一个答案,然后当这个答案被排序时,它说明我需要添加其余的问题。

function validation() { 

    var marks = parseInt($("#total-weight").text()); 
    var _qid = ""; 
    var _msg = ""; 

    var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ; 
    var questionsAdded = $('tr.optionAndAnswer').length; 

    var alertValidation = ""; 
    // Note, this is just so it's declared... 
    $("tr.optionAndAnswer").each(function() { 

     _qid = $("td.qid", this).text(); 
     _msg = "You have errors on Question Number: " + _qid + "\n"; 

     $(".numberAnswerTxtRow", this).each(function() { 
      var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length; 

      if ($(this).val() == 0) { 
       alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n"; 
      } else if (currenttotal < $(this).val()) { 
       alertValidation += "\n\u2022 You have selected less answers than the required amount\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 

    }); 



    if (alertValidation == "") { 
     if (questionsAdded < maxQuestions) { 
      _msg = ''; 
      alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:"); 
     } 
    } 

    if (alertValidation != "") { 
     alert(_msg + alertValidation); 
     return false; 
    } 

    return true; 
} 

下面是整个代码 - 在下面的代码,直到剩余的验证问题的数量已被第一传递它不显示其他的验证消息。就像我提到的唯一问题就是$(".numberAnswerTxtRow",this).each(function() {内验证,这也是出了其他功能的唯一功能,它包含了别人,如果:

function validation() { 

    var marks = parseInt($("#total-weight").text()); 
    var _qid = ""; 
    var _msg = ""; 

    var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ; 
    var questionsAdded = $('tr.optionAndAnswer').length; 

    var alertValidation = ""; 
    // Note, this is just so it's declared... 
    $("tr.optionAndAnswer").each(function() { 


     _qid = $("td.qid", this).text(); 
     _msg = "You have errors on Question Number: " + _qid + "\n"; 

     $(".textAreaQuestion", this).each(function() { 



      if (!this.value || this.value.length < 5) { 
       alertValidation += "\n\u2022 You have not entered a valid Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 

     $(".numberAnswerTxtRow", this).each(function() { 


      var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length; 

      if ($(this).val() == 0) { 
       alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n"; 
      } else if (currenttotal < $(this).val()) { 
       alertValidation += "\n\u2022 You have selected less answers than the required amount\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 

     }); 

     $(".txtWeightRow", this).each(function() { 


      if (!this.value) { 
       alertValidation += "\n\u2022 Please enter in a figure for Number of Marks for this Question\n"; 
      } 

      if (alertValidation != "") { 
       return false; //Stop the each loop 
      } 
     }); 


     if (alertValidation != "") { 
      return false; 
     } 
    }); 

    if (alertValidation == "") { 

     if (questionsAdded < maxQuestions) { 
      _msg = ''; 
      alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:"); 
     } 



    } 

    if (alertValidation != "") { 
     alert(_msg + alertValidation); 
     return false; 
    } 

    return true; 
} 

回答

1

如果我得到你的问题吧,这是你必须做什么:

$questions = array(); 

while ($selectedstudentanswerstmt->fetch()) { 

    //Check if the student data exist. 
    if (!isset($questions[$detailsStudentId])) { 
     $questions[$detailsStudentId] = array(
      'studentalias' => $detailsStudentAlias, 
      'studentforename' => $detailsStudentForename, 
      'studentsurname' => $detailsStudentSurname, 
      'questions' => array(); 
     ); 
    } 

    $questions[$detailsStudentId]['questions'][$detailsQuestionId] = array(
     'questionno'=>$detailsQuestionNo, 
     'content'=>$detailsQuestionContent, 
     'optiontype'=>$detailsOptionType, 
     'noofanswers'=>$detailsNoofAnswers, 
     'answer'=>$detailsAnswer, 
     'replytype'=>$detailsReplyType, 
     'questionmarks'=>$detailsQuestionMarks, 
     'studentanswer'=>$detailsStudentAnswer, 
     'responsetime'=>$detailsResponseTime, 
     'mouseclick'=>$detailsMouseClick, 
     'studentmark'=>$detailsStudentMark 
    ); 

} 

然后你可以遍历数组很容易:

foreach ($questions as $studentId => $studentData) { 
    echo $studentData['studentAlias'].' - '.$studentData['studentforename'].' '.$studentData['studentsurname'].'<br/>'; 

    foreach ($studentData['questions'] as $questionId => $questionData) { 
     echo '<strong>'.$questionData['questionno'].': '.$questionData['content'].'<br/>'; 
     echo $questionData['optiontype'].' - '.$questionData['noofanswers'].' - '.$questionData['answer'].' - '.$questionData['replytype'].' - '.$questionData['questionmarks'].'<br/>'; 
     echo $questionData['studentanswer'].' - '.$questionData['responsetime'].' - '.$questionData['mouseclick'].' - '.$questionData['studentmark'].'<br/>'; 
    } 
} 

这是你在找什么?

+0

我会给它一个测试,看看结果是什么,尝试回答问题的+1 – user1914374