I am working on a code for a quiz test, here
function run($id){
//Is this the first question ?
if($this->data){
$question_no = $this->Session->read('Test.qno'); //0
$last_answer = $this->Session->read('Test.last_answer');
$question_no = $question_no + 1; //1
$this->Session->write('Test.qno',$question_no); //Test.qno = 1
$this->Session->setFlash('last_answer'.$this->data['Test']['answer']);
$this->redirect($this->referer());
if($this->data['Test']['answer']==$last_answer){
$score = $this->Session->read('Test.score');
$score = $score + 1 ;
$this->Session->write('Test.score',$score);
$this->Session->setFlash('Correct answer');
}
}
$question_no = $this->Session->read('Test.qno'); //question_no =
if(!$question_no){
$question_no = 0;
$this->Session->write('Test.qno',$question_no);
$this->Session->write('Test.score',0);
}
$question = $this->Test->Question->find('first',array('conditions'=>array('Question.test_id ='=>$id),'offset'=>$question_no));
$answer = $question['Question']['answer'];
$this->Session->write('Test.last_answer',$answer);
if(empty($question)){
$score = $this->Session->read('Test.score');
$this->Session->setFlash('Your Score is '.$score);
$this->Session->write('Test.qno',0);
$this->redirect(array('controller'=>'States','action'=>'index'));
}
else{
$this->set(compact('question'));
}
}
here data comparison fails even though they both hold the same value can some one tell me why,here $last_answer is retrieved from session and holds a number.
$this->data[‘Test’][‘answer’] is taken from a form with radio button’s
Add
to confirm they really are the same.
Also make sure it’s not a problem with sessions (i.e. the condition is true, but the code inside doesn’t do anything): add
debug( "true" );inside the if block and confirm that you see the message.UPDATE after seeing the whole code:
You have a
$this->redirect()right before the if block! Of course the code you initially posted doesn’t do anything because you redirect the user to another page before the execution even reaches it.