hello everyone..i just wanna ask how i can increment the score after each answer given by the user and at last displaying result after questions finish. i want to store the result in database
here's my code which is working fine with checking questions and displaying result and then next question. moveover i want following things

1) to increment score after every correct answer
2) to store the final score in database with these foreign id's (course_id, evaluation_id, student_id)
3) to display final result and passing percentage after the last answer display
4) to check how much perentage is set for passing i have a column named pass percentage in evaluations table, which teacher have already fixed while making the quiz.

here's my code

start-course-evaluation.php

<?php include("../includes/config.php"); ?>
<?php
 if ($_SESSION["isstudent"])
   {
$cid= $_GET['id'];
$eid=$_GET['eid'];
$qid=$_GET['qid'];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db($dbname, $con);
$result1 = mysql_query("SELECT learningmaterial.id,learningmaterial.title,evaluation.id AS eid, evaluation.learningmaterialid FROM learningmaterial JOIN evaluation  ON learningmaterial.id=evaluation.learningmaterialid");

        while($row = mysql_fetch_array($result1))
        {
            $name=$row['title'];
            $evaluationid=$row['eid'];
        }
?>
<?php
$result = mysql_query("SELECT * FROM courses WHERE (id=".$cid.")");

        while($row = mysql_fetch_array($result))
        {
            $title=$row['title'];
        }
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<!--[if lt IE 9]>
  <script type="text/javascript" src="js/excanvas/excanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="js/spinners/spinners.min.js"></script>
<script type="text/javascript" src="js/lightview/lightview.js"></script>
<link rel="stylesheet" type="text/css" href="css/lightview/lightview.css"/>

<?php include("../includes/pre-header.php");?>
<title>Evaluation</title>
</head>
<body>
<div class="container">
<?php include("../includes/header.php"); ?>
<?php include("includes/nav.php"); ?>
<div id="maincontent">
<div class="span-24 last">
    <div id="breadcrumbs" class="mycss">
        <a href="index.php">Home</a> >
        <a href="my-courses.php">My Courses</a> >
        <a href="start-course.php"><?php echo $title; ?></a> >
        <a href="start-course-evaluation.php"><?php echo $name; ?></a> >
        Start Course

    </div>
    </div>
<?php include("includes/offered-courses-aside.php"); ?>
<br /> <br />
 <div class="span-18 last">
<h2 class="alt"><?php echo $title ?></h2>


<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
if (isset($_GET["qid"])){


    $query = "SELECT * FROM questions WHERE (evaluationid=".$eid.") AND id > ".$qid." ORDER BY id ASC LIMIT 1";
    $result = mysql_query($query);

    {
       echo "<form  action='display-answer.php' method='post'>";


$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
    $row = mysql_fetch_array($result);
    $qid=$row['id'];
    $question=$row['question'];
    $right_answer=$row['answer'];
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {    
    echo "<input type='radio' value='possible_opt_1' name='user-answer' checked />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='possible_opt_2' name='user-answer' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='possible_opt_3' name='user-answer' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='possible_opt_4' name='user-answer' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='possible_opt_1' name='user-answer' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='possible_opt_2' name='user-answer' />"; 
         echo  $row['possible_opt_2'];echo "<br />";    
         }

echo "<input type='hidden' name='id' value='".$cid."' />";             
echo "<input type='hidden' name='eid' value='".$eid."' />";
echo "<input type='hidden' name='qid' value='".$qid."' />"; 
echo "<input type='hidden' name='question' value='".$question."' />";
echo "<input type='hidden' name='right_answer' value='".$right_answer."' />";
echo "<input type='hidden' name='right_answer' value='".$right_answer."' />";
echo "<input type='submit' name='next' value='next' />";          

}

echo "</form>";
    }
}


else 
{
$query="SELECT * FROM questions WHERE (evaluationid=".$eid.")";
 $result = mysql_query($query);
echo "<form  action='display-answer.php' method='post'>";
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
    $row = mysql_fetch_array($result);
    $qid=$row['id'];
    $question=$row['question'];
    $right_answer=$row['answer'];
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {    
    echo "<input type='radio' value='possible_opt_1' name='user-answer' checked />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='possible_opt_2' name='user-answer' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='possible_opt_3' name='user-answer' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='possible_opt_4' name='user-answer' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='possible_opt_1' name='user-answer' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='possible_opt_2' name='user-answer' />"; 
         echo  $row['possible_opt_2'];echo "<br />";    
         }


echo "<input type='hidden' name='eid' value='".$eid."' />";
echo "<input type='hidden' name='qid' value='".$qid."' />"; 
echo "<input type='hidden' name='question' value='".$question."' />";
echo "<input type='hidden' name='right_answer' value='".$right_answer."' />";
echo "<input type='hidden' name='id' value='".$cid."' />"; 
echo "<input type='submit' name='next' value='next' />";          

}

echo "</form>";
}
mysql_close($con);
?>
<?php
/*
$correct = 0;
if(isset($_POST['answer'])) {
$selected_opt=$_POST['answer'];

if ($selected_opt== $row['answer'])
{
    $correct+= 10;
    echo "your answer is right";
}
else 
{
echo "your answer is wrong";    
}
}
*/
?>

</div>
</div>
<?php include("../includes/footer.php"); ?>
</div>
</body>

</html>
<?php
    }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");

    }
?>

display-answer.php

<?php include("../includes/config.php"); ?>
<?php 

 if ($_SESSION["isstudent"])
   {
?>

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<!--[if lt IE 9]>
  <script type="text/javascript" src="js/excanvas/excanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="js/spinners/spinners.min.js"></script>
<script type="text/javascript" src="js/lightview/lightview.js"></script>
<link rel="stylesheet" type="text/css" href="css/lightview/lightview.css"/>

<?php include("../includes/pre-header.php");?>
<title>Evaluation</title>
</head>
<body>
<div class="container">
<?php include("../includes/header.php"); ?>
<?php include("includes/nav.php"); ?>
<div id="maincontent">
<div class="span-24 last">
</div>

<?php include("includes/aside.php"); ?>
 <div class="span-18 last">
 <h1 class="alt">Result</h1>
<?php
$eid=$_POST['eid'];
$qid=$_POST['qid'];
$question=$_POST['question'];
$right_answer=$_POST['right_answer'];
$user_answer=$_POST['user-answer'];
$cid=$_POST['id'];



if ($right_answer==$user_answer)
{
    $score+=10;
    echo "<h1 class='mycss'>Your answer is right</h1>"; echo "<br />";
    echo "<h3 class='mycss' >your current score is '".$score."' </h3>";echo "<br />";
    $score ++;
}
else
{
    echo "<h2 class='mycss'>Your answer is wrong </h2>"; echo "<br />";
    echo "<h2 class='mycss'>The right answer is '".$right_answer."'</h2>"; echo "<br />";
    echo "<h1 class='mycss'>your current score is '".$score."'</h1>"; echo "<br />";
}


?>

<form method="get" action="start-course-evaluation.php">
<input type="hidden" name="qid" value="<?php echo $qid; ?>" />
<input type="hidden" name="id" value="<?php echo $cid; ?>" />
<input type="hidden" name="eid" value="<?php echo $eid; ?>" />
<input type="submit" class="mybutton" name="next" value="next" />
</form>
</div>
</div>
<?php include("../includes/footer.php"); ?>
</div>
</body>

</html>
<?php
    }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");

    }
?>

any kind of help would be appreciated :)

This is so basic, I have to assume you're trying to get us to do your homework for you.

@tiggsy no way... im just asking how would i do that, obviously no one have time to write code for others..i am new in php and just asking for help...

It is a lot of code to have a look at and a lot of includes which makes it very difficult to test. Also the structure of tables is not shown. And even if it were it is quite a project for someone to help here.

In principle I would have something like following tables:

Table: questions
Field          Type     Description
------------------------------------------------
id             int      ID
question       varchar  Question text
question_type  varchar  Question type (multichoice | true/false)
correct_answer tinyint  No of correct answers from possible_answers table

Table: possible_answers
Field          Type     Description
------------------------------------------------
q_id           int      ID of question from questions table
q_no           tinyint  No of possible answer for that question (e.g. 1, 2, 3, 4 ...)

Table: users
Field          Type     Description
------------------------------------------------
id             int      user ID
... (other user data)

Table: user_score
Field          Type     Description
------------------------------------------------
user_id        int      ID of a user (from the users table)
question_id    int      ID of a question (from the questions table)
answer_no      tinyint  No of answer the user chose (from the possible_answers table)

Now you can built all your logic arround these tables using correct queries:

  • to display a question
  • to display possible answers
  • to store chosen answer
  • to check whether the answer is correct
  • to calculate the score

I hope that helps.

@broj1 thankyou very much for your response
i think i haven't clarified my question cleanly...

i already have following tables

questions table

id|question|possible_opt_1|possible_opt_2|possible_opt_3|possible_opt_4|answer|evaluationid|type (mcqs/ true-false)

evaluation table

id|evaluationname|coursesid|learningmaterialid|description|pass percent (from 1-00)

i want to calculate the number of right answers and total score, and then calculate the percentage according to specified pass percent, pass percent may be zero to 100

i want to change this code as per above requirements...i hope i have explaind it clearly now...i wud be gratefull for any kind of help :)

if ($right_answer==$user_answer)
{
$score+=10;
echo "<h1 class='mycss'>Your answer is right</h1>"; echo "<br />";
echo "<h3 class='mycss' >your current score is '".$score."' </h3>";echo "<br />";
$score ++;
}
else
{
echo "<h2 class='mycss'>Your answer is wrong </h2>"; echo "<br />";
echo "<h2 class='mycss'>The right answer is '".$right_answer."'</h2>"; echo "<br />";
echo "<h1 class='mycss'>your current score is '".$score."'</h1>"; echo "<br />";
}

You have to know total maximum score which is total number of questions * 10 (I guess). Then when you have a score at particular question you just divide it with the total and multiply by 100. The result is the total percentage.

// ie you have 20 questions
$total_questions = 20;
$points_for_one_answer = 10;
$total_score = total_questions * $points_for_one_answer;
$score_percent = $score / $total_score * 100;

echo "your current score is $score_percent %";

$score is current score and I guess it is kept in session. It would be a good idea to display the number of answered questions and number of all questions:

echo "You have answered $number_of _answered questions out of $total_questions.";

thanx for the response, should i keep these in while loop??? to increment after every question??
and how i can get total number of questions from DB?
after evry answer i want to add up the score in the previous one.i am weak in syntax...

should i keep these in while loop??? to increment after every question?

I don't think so. Since every question is on separate page the the evaluation happens only once on each page (while the user is on that page).

and how i can get total number of questions from DB?

By querying questions using COUNT(*).

$query = 'SELECT COUNT(*) FROM questions';

If each user gets the same number of questions this is enough. If there are other conditions use WHERE clause like:

$query = 'SELECT COUNT(*) FROM questions WHERE courseid=' . $course_id;

@broj1 yes i waz asking this, i didnt knew about COUNT, thanx :)
yeah i got that but i want to + the previous score and the new one till the quiz fisnishes so i could print the total score..
if i set $score to 0 then after every question it turns zero and then it adds 10 if answer is true.. and then when it goes to next question and then comes on action script it sets score to zero again... i dont want that... i want to add up new score in previous one..

Save score for each question in a session where the key for an array is the question ID.

// store current score in an array of scores in a session
$_SESSION['score'][$id] = $points_for_current_answer;

Whenever you want to display the score, just add scores together:

$your_score = array_sum($_SESSION['score']);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.