[SOLVED] PHP calculation problem....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

[SOLVED] PHP calculation problem....

Post 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;
  }
?>
Last edited by sn202 on Wed Apr 06, 2005 9:45 am, edited 1 time in total.
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post 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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post 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.
Post Reply