Online Exam (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
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Online Exam (Problem)

Post by ashrafzia »

problem is to :

1- Select First Record (Question) from database.

2- Student will select his answer. (MCQ'S type ques and radio buttons having ans)

3- Then by clicking the next button, his answer will be saved in the database and the

Next Record (Question) will be selected and displayed and so on upto End of Records

(Questions).

4- Upto step 2 and half of step 3 i am OK. But half of step 3 i mean selection of

Next Record ( Question ) is problem?

5- I don't know to put what condition to increment the qno (i-e Question No)

for displaying Next Reocrd (Next Question)

"Selecte * from paper where qno=....."


One point must be kept in mind that if there are 30 questions in database then all of them will be selected by incrementing the previous question value (i-e qno (question no)) and all this process will be dynamic , by dynamic i mean that by clicking the next button the ans will be saved in database and next will be selected and again on next click the same file will be selected but with an incremented value of qno (i-e question no)

i hope you get my point

Hope to be replied
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what exactally is your question? is it what your query should be to select the next question? have a hidden field in the form of the question number that you are on then just do

Code: Select all

$next = $_POST['next'];

$query = '
SELECT
  *
FROM
  table_name
WHERE
  qno = "'.$next.'"
';
is that what you wanted?
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

1st of All thanx for Reply:

Actuallly my question is :

1- How to select next record (question) from database within the same file ?

In other words when the student selects his answer and clicks next button

his answer will be saved in the database and the next question will be retrieved

within the same file.

Now why i wanna use one file ? bcoz if i have 30 qestions in my database then

it will be very bad programming to create 30 pages for retriewing those questions.

I hope you understand......

waiting for your reply..
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

I have the following Code :

This is test.php File:

Code: Select all

<?
include "conn.php";

$j=1;
$j="$_post[v1]";


$sql=" SELECT * from paper WHERE prog='$_POST[prog]' and sem='$_POST[sem]' and sub='$_POST[sub]' and password='$_POST[psd]' and qno=$j ";

$result=mysql_query($sql,$conn) or die (mysql_error());

echo "<center><h2><u>:: Paper ::</u></h2></center>";
while ($row=mysql_fetch_array($result))
	{
		$qno=$row['qno'];
		$ques=$row['questions'];
		$c1=$row['c1'];
		$c2=$row['c2'];
		$c3=$row['c3'];
		$true=$row['ans'];
		//$result=mysql_query($sql,$conn) or die(mysql_error());
		
		 echo "<form method=\"post\" action=\"test.php\">
		       <input type=\"hidden\" name=\"h1\" value=\"$_POST[prog]\">
			   <input type=\"hidden\" name=\"h2\" value=\"$_POST[sem]\">
			   <input type=\"hidden\" name=\"h3\" value=\"$_POST[sub]\">
		      
			  <table border=0><tr><td bgcolor=\"#FFFF9C\">Q:$qno</td>
			  <td width=740 bgcolor=\"#CCFFCC\">$ques</td></tr></table>
		      <input type=\"radio\" value=\"$c1\" name=\"std_ans\">$c1  
		      <input type=\"radio\" value=\"$c2\" name=\"std_ans\">$c2  
			  <input type=\"radio\" value=\"$c3\" name=\"std_ans\">$c3<br><br>
			  
			  <input type=\"hidden\" value=\"$_POST[id]\" name=\"id\">
			  <input type=\"hidden\" value=\"$_POST[name]\" name=\"name\">
			  <input type=\"hidden\" value=\"$_POST[prog]\" name=\"prog\">
			  <input type=\"hidden\" value=\"$_POST[sem]\" name=\"sem\">
			  <input type=\"hidden\" value=\"$_POST[sub]\" name=\"sub\">

// This hidden fields contains the question number                          

                          <input type=\"hidden\" value=\"$qno+1\" name=\"v1\">
			  <input type=\"submit\" name=\"submit\" value=\"Next\">  ";
    		 	
	}


$sql =" INSERT into result (std_id, name, prog, sem, sub, qno, std_ans) VALUES
        ('$_POST[id]', '$_POST[name]', '$_POST[prog]', '$_POST[sem]', '$_POST[sub]', $qno, $std_ans)  ";
$result=mysql_query($sql);

?>
In the action there's the name of the same file ie test.php.

Problems:

1- When i echo the $_post[v1] it displays 1+1 . it do not perform arithmetic operation

over it.

This is the method that i am trying

In this file i want my whole process to be done.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

When using arrays, the indices must be referenced within their repsective context.

In you example above, you are referring to the indices using their assigned String value, thus you must refer them as Strings by using quotes, such as $_POST['v1'] instead of $_POST[v1]

When using the above within a quoted line, like you have for the SQL Statements, either concatenate them, e.g:

Code: Select all

<?php


$sql=" SELECT * from paper WHERE prog='" . $_POST['prog'] . "' and sem='" . $_POST['sem'] . "' and sub='" . $_POST['sub'] . "' and password='" . $_POST['psd'] . "' and qno=$j ";

?>
Or, enclose them in braces:

Code: Select all

<?php

$sql=" SELECT * from paper WHERE prog='{$_POST['prog']}' and sem='{$_POST['sem']}' and sub='{$_POST['sub']}' and password='{$_POST['psd']}' and qno=$j ";

?>
:)
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

You are definetly right but thats not the main problem the first record is still coming from the database but the next one doesn't .
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

To loop through records:

Code: Select all

<?php

while ($row = mysql_fetch_array($result)) {

  //do something with $row array

}

?>
:)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

simple idea of what you need to do

Code: Select all

if (empty($_POST['qno']))
  $qno = 1;
else
  $qno = $_POST['qno'];

$query = '
  UPDATE
    table_name
  SET
    answer = "'.mysql_real_escape_string($_POST['answer']).'"
  WHERE
    qno = "'.$qno--.'"
  AND
    student = "'.$_SESSION['student'].'"
';

$do_query = mysql_query($query) or die(mysql_error());

//build the query
$query = '
  SELECT
    *
  FROM
    table_name
  WHERE
    qno = "'.$qno.'"
';

$do_query = mysql_query($query) or die(mysql_error());

$info = mysql_fetch_assoc($do_query);

echo $info['question'].'<br>';//echo out the current question

?>
<form action="thispage.php" method="post">
<input type="text" name="answer">
<input type="hidden" name="qno" value="<?= $qno++ ?>">
<input type="submit" name="submit" value="submit">
</form>
Post Reply