测验示例中会话的条件

  • 本文关键字:会话 条件 php
  • 更新时间 :
  • 英文 :


这是一个测验的过程示例.php,一切都很好,但是如果用户回答正确问题并再次返回问题并再次正确回答。分数添加更多,这是我的会话问题?

if(isset($_POST['submit'])){
//get values
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number + 1 ;
//get NOQ
$query_NOQ = "select * from questions";
$result_NOQ = $connection->query($query_NOQ) or die($connection->error.__LINE__);
$NOQ = $result_NOQ ->num_rows;
$query_co = "select * from choices where question_number = $number AND is_correct = 1";
$result_co = $connection->query($query_co) or die($connection->error.__LINE__);
$correct_row = $result_co->fetch_assoc();
$correct_answer = $correct_row['id'];
if($selected_choice == $correct_answer){
$_SESSION['score']++;
}
if($number == $NOQ){
header("Location:final.php");
}  else {
header("Location:questions.php?n=".$next);
}

你会做这样的事情:

//Check if there is something for that question
if(!isset($SESSION['answered_' .$number])){
if($selected_choice == $correct_answer){
$_SESSION['score']++;
}
//Set something so if they come back nothing happens to score
$SESSION['answered_' .$number] = TRUE;
} else {
//Could put some logic here that they already answered this one?
}

if($number == $NOQ){
header("Location:final.php");
}  else {
header("Location:questions.php?n=".$next);
}

您必须添加功能以检查用户是否回答了相同的问题?

if(isset($_POST['submit'])){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number + 1 ;
//get NOQ
$query_NOQ = "select * from questions";
$result_NOQ = $connection->query($query_NOQ) or die($connection->error.__LINE__);
$NOQ = $result_NOQ ->num_rows;
/*check here that user already answer this question from database (add userid in the DB or join with userid for identical purpose 
of related user*/
$check = sonefunc();
if($check==true){
$query_co = "select * from choices where question_number = $number AND is_correct = 1";
$result_co = $connection->query($query_co) or die($connection->error.__LINE__);
$correct_row = $result_co->fetch_assoc();
$correct_answer = $correct_row['id'];
if($selected_choice == $correct_answer){
$_SESSION['score']++;
}
if($number == $NOQ){
header("Location:final.php");
}  else {
header("Location:questions.php?n=".$next);
}
}else{
echo "Already answered this question";
}
}

最新更新