Page 1 of 1

[SOLVED] PHP calculation problem....

Posted: Tue Apr 05, 2005 6:43 pm
by sn202
Hi all,

Basically I'm making an Assessment and feedback system, which allows instructors to create tests and feedback for each answer, which are then taken by students. Now I've got the whole system working, the instructor sub system consists of forms to create questions and feedback all of which are then stored in a backend MySQL database. And the learner subsystem basically deals with displaying tests and grading the answers given by the student. And this is where my problem is.

In order to mark the students response to the multiple-choice questions, each answer has a true or false value related to it. So If the student gets an answer correct the variable $score is incremented by 1. Now score is passed from a hidden text box on page show_test.php to page show_feedback.php where this calculation is carried out and $score echoed (code shown below:) however the score is not being echoed.

Any ideas?

Regards,

Simon.

Code: Select all

<?php
  $answerno 	= $_POST['answer'];
  $question 	= $_POST['questionno'];
  $testno	= $_POST['testno'];
  $scores	= $_POST['score'];
  #connect to database
  $conn = @mysql_connect( "****", "****", "****" )
  or die( "could not connect" );
  #select the specified database
  $rs = @mysql_select_db ( "db_sn202", $conn )
  or die( "could not select database" );
  #create the sql query
  //since there is no feedback 1, must change 1 to blank
  $newno = ($answerno == 1 ? '' : $answerno);
  $sql = "SELECT `feedback".$newno."` FROM `question` WHERE `testno` = '".$testno."' AND `questionno` = '$question'";
  $result = mysql_query($sql) or die(mysql_error());
  if (mysql_num_rows($result) > 0)
  {
  	while ($row = mysql_fetch_assoc($result))
  	{
  		echo $row['feedback'.$newno].'<br />';
  	}
  }
  else
  {
  	echo 'No Feedback Found';
  }
  $sql2="select `truefalse".$newno."`from `question` where `testno` = '".$testno."' and `questionno` = '$question'";
  $rs = mysql_query($sql) or die(mysql_error());
  $row=mysql_fetch_object($rs);
  $answer = $row->truefalse.$newno;
  Switch($answer)
  {
  	case "True":
  	$score = $scores + (1);
  	break;
  	case "False":
  	$score = $scores;
  	break;
  	echo $score;
  }
?>

Posted: Wed Apr 06, 2005 8:32 am
by sn202
Ok, it is echoing $score now, however it is echoing $score as 0 even though the answer is correct and it should be 1. So its not carrying out the calculation. Any ideas?

Posted: Wed Apr 06, 2005 8:38 am
by CoderGoblin
Quick guess...

Code: Select all

switch($answer)
{
  case "True":
    $score = $scores + (1);
    break;
  case "False":
    $score = $scores;
    break;
  default:
    die("Illegal parameter);
}
echo $score;
Please note switch should be in lowercase. Also moved the echo outside the switch.

Posted: Wed Apr 06, 2005 8:42 am
by timvw
do the usual debugging stuff at the beginning of your script....

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
first guess is that $answer = $row->truefalse.$newno; does not what you expect...

i would store the score in a session... not in a hidden form field (this way the student can't alter it)

Posted: Wed Apr 06, 2005 9:44 am
by sn202
Solved, it was just a stupid thing, was running $sql instead of $sql2 and as such "trufalse" was not being defined. Now I've got it working though I will change it to use sessions to stop tampering...

Cheers,

Simon.