我有一个fb测验应用程序,如果我们检查正确的问题,然后转到下一个问题但是当我多次更改选项时显示负分数时工作正常
我正在调用ajax onchange选择更新分数页面并在会话中存储分数的选项
在这个代码中,如果我们选择正确的答案然后错误,那么它会正常工作,但是当选择错误时,我们会因为
而得到负分$_SESSION['score']--;
如何解决这个问题?
// check if this option is correct for current question
$check_anwer_query = mysql_query("select * from tbl_option where ques_id=$ques_id and opt_id=$opt_id") or die(mysql_error());
if(mysql_num_rows($check_anwer_query))
{
$rs = mysql_fetch_array($check_anwer_query);
if($rs['opt_value']==1)
{
$_SESSION['score']++;
//$_SESSION[$ques_id]='1';
}
else if($_SESSION['lastq']==$ques_id) // check if option is changed,
{
$_SESSION['score']--;
}
if(isset($_SESSION['user']))
{
mysql_query("delete from tmp_quiz where ques_id=$ques_id and uid='".$_SESSION['user']."'") or die(mysql_error());
if($rs['opt_value']==1)
{
mysql_query("insert into tmp_quiz(ques_id,opt_id,uid,is_correct) values($ques_id,$opt_id,'".$_SESSION['user']."',1)") or die(mysql_error());
}
else
{
mysql_query("insert into tmp_quiz(ques_id,opt_id,uid,is_correct) values($ques_id,$opt_id,'".$_SESSION['user']."',0)") or die(mysql_error());
}
}
else
{
mysql_query("delete from tmp_quiz where ques_id=$ques_id and tmp_id='".$_SESSION['tmp_id']."'") or die(mysql_error());
mysql_query("insert into tmp_quiz(ques_id,opt_id,tmp_id) values($ques_id,$opt_id,'".$_SESSION['tmp_id']."')") or die(mysql_error());
}
}
$_SESSION['lastq'] = $ques_id; // assign current question as last question answred
?>
答案 0 :(得分:1)
您不应该进行相对计算,因为这需要您确定UI和会话之间共享的状态是100%同步并且没有用户操作无序到达(我猜你的问题在哪里) )。
相反,请在会话中保存所有选定的答案。当想要显示用户的分数时,请浏览所有选择的答案并根据该分数计算一个数字。每次 你想要显示用户的分数,所以分数总是最新且准确。
答案 1 :(得分:1)
//Logic for Score Start
$rs = mysql_fetch_array($check_anwer_query);
if($rs['opt_value']==1)
{
$_SESSION['score']++;
$_SESSION["test_".$ques_id]='1'; // if Answer is Correct Set Session
}
elseif($_SESSION['lastq']==$ques_id) // check if option is changed
{
if(isset($_SESSION["test_".$ques_id])) { // if Last Ansewer is same as current question and last answer is yes then substract score
$_SESSION['score']--;
unset($_SESSION["test_".$ques_id]); } // Unset That Session
}
elseif(isset($_SESSION['test_'.$_SESSION['lastq']])) // If Last Question Session is still exists Then Unset That Question Session
{
unset($_SESSION['test_'.$_SESSION['lastq']]);
}
//Logic for Score End