Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!
@  Ritesh : (27 March 2014 - 10:46 AM) :(
@  Ritesh : (27 March 2014 - 10:46 AM) He Is Busy With His Patients.
@  yordan : (26 March 2014 - 08:12 PM) Ahsani, Where Are You?

Photo
- - - - -

How To Create An Online Timed Test With PHP? Can we do a online test with a counter(from 20 second) php for a php-n


18 replies to this topic

#1 thenumberone

thenumberone

    Newbie [ Level 2 ]

  • Members
  • 28 posts

Posted 10 May 2006 - 12:01 PM

Is there anyone got a online timed test script?Or anyone knows how to create this script?
I wait your answers and all php programmers.

#2 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 11 May 2006 - 12:48 AM

Is there anyone got a online timed test script?Or anyone knows how to create this script?
I wait your answers and all php programmers.

Hi, interesting script you propose, i saw sometime ago something like that, but i dont remember if it was in php, i think it not be difficult to develop, so i will do it in my spare time in a few hours.

Best regards,

Edited by TavoxPeru, 11 May 2006 - 01:53 AM.


#3 Quatrux

Quatrux

    the Q

  • [HOSTED]
  • 1,669 posts
  • Gender:Male
  • Location:Lithuania, Vilnius
  • Interests:PHP, MySQL, Oracle, PL/SQL, HTML, CSS, Javascript, jQuery, C# Computers, Alternative OS, Amiga, MorphOS, Beer, Friends, Linux, KDE..
  • myCENTs:30.76

Posted 11 May 2006 - 11:31 AM

You can do it with php and if you want you also could add an javascript real time, so you want to know how it works ? Well, one way is to make it with sessions, say you have 10 pages of questions.. so you start a sessions and set the start time when the user clicks start and always check the value and set the finish time and get the period of the time. Simple maths :D Also, you save the answers in the super global $_SERVER; and check them after the test is done or whatever. There might be different ways to do it, saving to a database or files as temporary data.. I would use sessions, but if the user does not have cookies enabled - he might have some problems.. :P

#4 thenumberone

thenumberone

    Newbie [ Level 2 ]

  • Members
  • 28 posts

Posted 11 May 2006 - 11:56 AM

You can do it with php and if you want you also could add an javascript real time, so you want to know how it works ? Well, one way is to make it with sessions, say you have 10 pages of questions.. so you start a sessions and set the start time when the user clicks start and always check the value and set the finish time and get the period of the time. Simple maths :D Also, you save the answers in the super global $_SERVER; and check them after the test is done or whatever. There might be different ways to do it, saving to a database or files as temporary data.. I would use sessions, but if the user does not have cookies enabled - he might have some problems.. :P

As I mentioned in database part is there anyone got this script?Is there anyone possible to write this script?
I want to explain my problem again.I made a php-nuke website.I have so many users.My users wanted a online timed(counts from 20 seconds)test(multiple choices).(like who wants to be a millionaire).I made the login and register system.But I have problems in test page stage.Users will be send questions to our database.And admin will control these questions.He will save it to our database or delete it from our database.And users will be race in a day only five times.And also they must be answer the questions in 20 seconds.There will be shown a counter in the question page that counts from 20 second.Also unregistered users can not be reach the test page by anyway.Or if they try this the page must be redirect him to register page.Only registered users can answer these questions.I think it will be possible by Session_start() and session_register and session_destroy codes or by setcookie.I have little time to this because of my university education.But I think you will help me.I search all internet pages but I couldn't find any useful scripts about this online timed test.But I will have more time in the summer holiday.Is it impossible?I think impossible is nothing!Can we put time script by javascript?But how can we connect it with our database?I mean that out of time there will be a page shown that echo like this "Sorry you answered false.Please restart our test...." or answered in 20 seconds "Congratulations.Please click to next question".
Also istatistics of other users must be shown in the page.Also after all true answers the users score must be increase.And after all wrongs the score must be decrease.(I did all of this with Delphi but i can not put it on my website.)
So,I wait your ideas or helps or scripts.
Regards;

#5 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 19 May 2006 - 05:10 AM

Hi, i just start this project in my spare time i have today, you can see it Here thanks to astahost.com.

It is not finished yet, but for a start is ok. First of all, you must register to access the trivia script, so, when you click in the above link you access the registration process, complete the form and if the data is ok the script will send you your access data to the email you give. This email includes an activation link that you must click to activate your account and after that you can login, if you dont do it you cannot access the trivia script.

The trivia script will present user-defined trivia questions along with choices and determine whether or not the visitor entered the correct answer. Features randomized question and choice order along with 100% templates. All the question, choices and answers are stored in a simple text file.

So, as you can see, this is not the best solution, in the next version i will use a database to store the questions, choices and answers as other information, and also implement the 20 seconds counter.

Best regards,

#6 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 25 May 2006 - 02:31 AM

Well, my new version is partially finished, i just implement the database because i have a qUestion related with the time counter, tell me, the 20 seconds counter must be for all questions or for each question????

best regards,

#7 thenumberone

thenumberone

    Newbie [ Level 2 ]

  • Members
  • 28 posts

Posted 25 May 2006 - 06:33 AM

Well, my new version is partially finished, i just implement the database because i have a qUestion related with the time counter, tell me, the 20 seconds counter must be for all questions or for each question????

best regards,

the 20 seconds counter must be for each questions!

#8 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 26 May 2006 - 08:54 AM

the 20 seconds counter must be for each questions!

Ok, and a test how many question should include???? to be practical i will include only 5 questions for test, and because it will be a test, i will not do any type of validation.

best regards,

#9 thenumberone

thenumberone

    Newbie [ Level 2 ]

  • Members
  • 28 posts

Posted 26 May 2006 - 09:07 AM

Ok, and a test how many question should include???? to be practical i will include only 5 questions for test, and because it will be a test, i will not do any type of validation.

best regards,

How can I get the script from you?
I did what you said but ı can't get.
Regards.

#10 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 28 May 2006 - 02:08 AM

How can I get the script from you?
I did what you said but ı can't get.
Regards.

Yes, sorry for that, right now i'm very busy with a lot of work and in a few hours i will try to upload a new version with some changes. Well, i just upload the new version, it works but i have some trouble with date's checking, also im not finished yet the 20s time counter, i think im in the correct way.

When i finish i will post all the code.

best regards,

Edited by TavoxPeru, 28 May 2006 - 07:38 AM.


#11 thenumberone

thenumberone

    Newbie [ Level 2 ]

  • Members
  • 28 posts

Posted 29 May 2006 - 06:41 AM

Yes, sorry for that, right now i'm very busy with a lot of work and in a few hours i will try to upload a new version with some changes. Well, i just upload the new version, it works but i have some trouble with date's checking, also im not finished yet the 20s time counter, i think im in the correct way.

When i finish i will post all the code.

best regards,

Thank u very much for your interestings about my question.

#12 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 03 June 2006 - 05:10 AM

Thank u very much for your interestings about my question.

You're welcome, BTW after a few days without coding -too much work :-)- i just correct the trouble with the dates checking and will continue with the rest of the problem. So, i hope that i will finish it this weekend.

best regards,

#13 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 15 June 2006 - 11:00 AM

Well, after some days i return to this script -i'm very busy with a lot of work- so, i just upload the new version, it works but i have some trouble with the 20s time counter, the problem is that the form.submit() function doesnt work when the time is over.

Here's the problematic code:
$formoutput = "<form name=\"form\" method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\"><input type=\"hidden\" name=\"question\" value=\"" . $TheQuestion . "\">";
$formoutput .= 
"Time Left: <input type=\"text\" name=\"seconds\" size=\"3\"><br /><br />\n" .
"<script type=\"text/javascript\">\n" .
"var myTime = 20;\n" .
"function countDown() { \n" .
"\tdocument.form.seconds.value = myTime; \n" .
"\tif (myTime == 0) { \n" .
"\t\t// http://www.gigasoft.astahost.com/trivia/trivia.php;\n" .
"\t\t//clearTimeout(timer1);\n" .
"\t\t//document.form.submit()\n" .
"\t\tlocation.href ='" . $_SERVER["PHP_SELF"]. "';\n" .
"\t}\n" . 
"\telse if (myTime > 0) { \n" .
"\t\tmyTime--; \n" .
"\t\tsetTimeout(\"countDown()\",1000); \n" .
"\t}\n" .
"}\n\n" .
"countDown();\n" .
"</script>"; 
$formoutput .= $choiceoutput;
$formoutput .= "<br><input type=\"submit\" value=\"Answer\" name=\"submit\"></form>";

Best regards,

#14 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 15 June 2006 - 11:31 AM

I saw this question in the PHP section and though the first answer should be no you can not use PHP to create a timed event for such a script. PHP is server side and only reacts when the server is accessed. The browser (client side is always open) so the only way to write a time dependant script would be with JavaScript. So technically you can not create a PHP script for something like a clock or a counter that counts down. That would be best achieved by using JaavaScript or JScript.

Of course you can user PHP to manage the database or flat file to store questions and things like that but using PHP alone will not do the job.

#15 MS2

MS2

    Newbie [ Level 1 ]

  • Members
  • 7 posts

Posted 16 June 2006 - 11:08 AM

So your trivia questionsmust be answered within 20 sec.

At page YY:
Ask question + register start time
-Make sure a JS refreshes after 20 sec to page XX.
-And make sure answers are posted to page XX.

Page XX checks for an correct answer AND checks if the registered start time in php is within the 20 sec (JS can be easily disabled and needs double checking)

Edited by MS2, 16 June 2006 - 11:11 AM.


#16 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 18 June 2006 - 09:36 AM

So your trivia questionsmust be answered within 20 sec.

At page YY:
Ask question + register start time
-Make sure a JS refreshes after 20 sec to page XX.
-And make sure answers are posted to page XX.

Page XX checks for an correct answer AND checks if the registered start time in php is within the 20 sec (JS can be easily disabled and needs double checking)

Thanks, i do something like that with no goods results in the timer, so i follow your advice and redirect to XX page and THAT'S IT :( .

I will post all the code tomorrow.

CLICK HERE TO PLAY TRIVIA

Best regards,

#17 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 19 June 2006 - 05:19 AM

Well, as i promised in my last post yesterday here is the code of the script, it is not complete yet because i will make some improvements but the basical functioning is complete. Today is my happybirthday so i only post the database tables and the config files:

DB Tables Files:

CREATE TABLE registration (
id int(10) unsigned NOT NULL auto_increment,
name varchar(45) NOT NULL default '',
email varchar(100) NOT NULL default '',
nickname varchar(15) NOT NULL default '',
password varchar(32) NOT NULL default '',
is_activated char(1) NOT NULL default '0',
activation_code varchar(25) NOT NULL default '',
regdate date NOT NULL default '0000-00-00',
last_login date NOT NULL default '0000-00-00',
test_day tinyint(1) unsigned NOT NULL default '0',
points int(10) NOT NULL default '0',
PRIMARY KEY (id)
) TYPE=MyISAM COMMENT='Trivia Users';


CREATE TABLE question (
id smallint(5) unsigned NOT NULL auto_increment,
question varchar(255) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM COMMENT='Trivia Questions';

CREATE TABLE answer (
idq smallint(5) unsigned NOT NULL default '1',
id tinyint(3) unsigned NOT NULL auto_increment,
answer varchar(255) NOT NULL default '',
correct tinyint(1) unsigned NOT NULL default '0',
PRIMARY KEY (idq,id)
) TYPE=MyISAM COMMENT='Trivia Answers' ;

config.php: general configuration file
<?php
$db_user = "your_db_user"; 
$db_password = "your_db_userpassword";
$database = "your_db";
$server = "localhost";
$questions_test=5; // number of questions per test
$total_tests=5; // number of tests per day

// connect to server
mysql_connect($server, $db_user, $db_password) or die ("Could not connect to the Server!" . mysql_errno() . ": " . mysql_error() );

// select database
mysql_select_db($database) or die ("Could not select the Database!" . mysql_errno() . ": " . mysql_error() );
include('check_login.php');
?>

check_login.php: user checking configuration file
<?php
/* check login script, included in config.php. */
session_start();

if (!isset($_SESSION['nickname']) || !isset($_SESSION['password'])) {
	$logged_in = 0;
	return;
} else {
	// remember, $_SESSION['password'] will be encrypted.
	if(!get_magic_quotes_gpc()) {
		$_SESSION['nickname'] = addslashes($_SESSION['nickname']);
	}

	// addslashes to session username before using in a query.
	$pass=mysql_query("SELECT password FROM registration WHERE nickname='".$_SESSION['nickname']."'");

	if(mysql_num_rows($pass) != 1) { 
		$logged_in = 0;
		unset($_SESSION['nickname']);
		unset($_SESSION['password']);
		// kill incorrect session variables.
	  } 

	$db_pass = mysql_fetch_array($pass);

	// now we have encrypted pass from DB in 
	//$db_pass['password'], stripslashes() just incase:

	$db_pass['password'] = stripslashes($db_pass['password']); 
	$_SESSION['password'] = stripslashes($_SESSION['password']);

	//compare:

	if($_SESSION['password'] == $db_pass['password']) { 
		// valid password for username
		$logged_in = 1; // they have correct info in session variables.
	} else {
		$logged_in = 0;
		unset($_SESSION['nickname']);
		unset($_SESSION['password']);
		// kill incorrect session variables.
	}
}
// clean up
unset($db_pass['password']);
$_SESSION['nickname'] = stripslashes($_SESSION['nickname']);
?>

To be continued...

Best regards,

Edited by TavoxPeru, 26 June 2006 - 03:12 AM.


#18 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 26 June 2006 - 04:24 AM

login.php: trivia's login file.
<?php
require 'config.php';
if($logged_in == 1) {
	die('You are already logged in, '.$_SESSION['nickname'].'. <a href="logout.php">Click here to logout</a>.');
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Trivia - Login</title>
<style type="text/css">
<!--
body { margin:10;background-color:#131313; font-family:Verdana, Arial; font-weight:bold; font-size:9px; color:#FFFFFF; }
.register_box { border: 1px solid #323232; background-color: #202020; font-family: Verdana, Arial; font-weight: bold; font-size: 9px; color: #FFFFFF; } 
P { font-family:Verdana, Arial; font-size:11px; color:white; text-align:justify }
background-color: #009900; cursor: pointer; }
.legend {font-family: Verdana, Arial; font-size:8pt; font-weight:bold; color:white; width:50%; text-align:right; padding-left:2px; padding-bottom:3px; padding-top:3px }
.title { font-family: Verdana, Arial; font-size:14pt; font-weight:bolder; color:white; text-align:center; width:100%; padding:10px }
.field {font-family: Verdana, Arial; font-size:8pt; color:white; text-align:left; width:50%; padding-left:2px;	padding-right:2px; padding-bottom:3px; padding-top:3px }
//-->
</style>
</head>
<body>
<?php

if (isset($_POST['submit'])) { // if form has been submitted

	/* check they filled in what they were supposed to and authenticate */
	if(!$_POST['nname'] || !$_POST['passwd']) {
		die('You did not fill in a required field.');
	}

	// authenticate.
	if (!get_magic_quotes_gpc()) {
		$_POST['nname'] = addslashes($_POST['nname']);
	}
	$Sqlcheck="SELECT nickname, password, last_login, test_day FROM registration WHERE nickname='" . $_POST["nname"] . "'";
	$check=mysql_query($Sqlcheck) or die(mysql_error());
	if(mysql_num_rows($check)<1) { 
		die("That nickname does not exist in our database. Please <a href=\"registration.php\">click here</a> to register and try again");
	} 
	$info = mysql_fetch_array($check);

	// check passwords match
	$_POST['passwd'] = stripslashes($_POST['passwd']);
	$info['password'] = stripslashes($info['password']);
	$_POST['passwd'] = md5($_POST['passwd']);

	if ($_POST['passwd'] != $info['password']) {
		die('Incorrect password, please try again.');
	}

	// if we get here username and password are correct, 
	// check last login date and set last login date, number of plays  
	// update database and register session variables.

	$date=date("Y-m-d",strtotime("now"));
	$times_play=0; //number of tests played today
	$last=$date; // default last login date
	$last_login_date = $info['last_login']; // user's last login date
	if($last_login_date == $date) {
		$times_play=$info['test_day'];
		$last = $last_login_date;
	}

	$update_login = "UPDATE registration SET last_login =now(), test_day=".$times_play. " WHERE nickname = '".$_POST['nname']."'";
	mysql_query($update_login) or die(mysql_error());

	$_POST['nname'] = stripslashes($_POST['nname']);
	$_SESSION['nickname'] = $_POST['nname'];
	$_SESSION['password'] = $_POST['passwd'];
	$_SESSION['tests_count'] = $times_play;
	mysql_close();

	// check number of test played by the user
	if ($_SESSION['tests_count']==5) {
		$template = "template4.html";
		if(!$output = file($template)) die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
		$output = implode("\n", $output);
		$output = str_replace("%%NICKNAME%%", $_SESSION['nickname'], $output);
		echo $output;
		exit();
	}
?>
<h1>Logged in</h1>
<p>Welcome back <?php echo $_SESSION['nickname']; ?>, you are logged in and ready to play our <a href="trivia.php">TRIVIA</a>.<br /><br /><a href="logout.php">Click here to logout</a>.</p>
<?php
} else {	// if form hasn't been submitted
?>
<div align="center">
	<center>
		<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
			<table align="center" border="0" width="60%" cellspacing="0" cellpadding="0">
				<tr>
					<td colspan="2" class="title">LOGIN</td>
				</tr>
				<tr>
					<td class="legend">Nickname</td>
					<td class="field"><input class="register_box" TYPE="TEXT" NAME="nname" SIZE="10" MAXLENGTH="15" tabindex="1"></td>
				</tr>
				<tr>
					<td class="legend">Password</td>
					<td class="field"><input class="register_box" TYPE="PASSWORD" NAME="passwd" SIZE="10" MAXLENGTH="32" tabindex="2"></td>
				</tr>
				<tr>
					<td width="100%" colspan="2" align="center" style="padding-top:3px;padding-bottom:3px">
					<input class="register_box" type="submit" value="Login" name="submit" tabindex="3" > 
					<input class="register_box" type="reset"  value="Reset" tabindex="4">
					</td>
				</tr>
			</table>
		</form>
	</center>
</div>
</body>
</html>
<?php
}
?>
trivia.php: The Trivia's main file where we show the questions and its corresponding result using a simple html template file that include a script in javascript to control the time.
<?php
require("config.php"); 
if ($logged_in == 0) {
	die('Sorry you are not logged in, this area is restricted to registered members. <a href="login.php">Click here</a> to log in.');
}

# check number of plays per day
if ($_SESSION['tests_count']==5) {
	$template = "template5.html";
	if(!$output = file($template)) die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
	$resultoutput="";
	$formoutput="";
	$output = implode("\n", $output);
	$output = str_replace("%%RESULT%%", $resultoutput, $output);
	$output = str_replace("%%NICKNAME%%", $_SESSION['nickname'], $output);
	$output = str_replace("%%FORM%%", $formoutput, $output);
	echo $output;
	exit();
}

# check number of questions per play
if (!isset($_SESSION['questions_count'])) {
	$_SESSION['questions_count']=1;
} else {
	if($_SESSION['questions_count']<$questions_test) {
		$_SESSION['questions_count']++;
	}
	else {
		unset($_SESSION['questions_count']);
		$update_login = "UPDATE registration SET test_day=test_day+1 WHERE nickname = '".$_SESSION['nickname']."'";
		mysql_query($update_login) or die(mysql_error());
		$SqlTestcheck="SELECT test_day FROM registration WHERE nickname='" . $_SESSION["nickname"] . "'";
		$Testcheck=mysql_query($SqlTestcheck) or die(mysql_error());
		$Testinfo = mysql_fetch_array($Testcheck);
		$_SESSION['tests_count'] = $Testinfo["test_day"];
		$template = "template3.html";
		if(!$output = file($template)) die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
		$resultoutput="";
		$formoutput="";
		$output = implode("\n", $output);
		$output = str_replace("%%RESULT%%", $resultoutput, $output);
		$output = str_replace("%%NICKNAME%%", $_SESSION['nickname'], $output);
		$output = str_replace("%%PLAYSLEFT%%", ($total_tests - $_SESSION['tests_count']), $output);
		$output = str_replace("%%FORM%%", $formoutput, $output);
		echo $output;
		mysql_close();
		exit();
	}
}

#$template is the template file the script will use to
#format the form. 
$template = "formtemplate.html";

#load the template file
if(!$output = file($template))
  die("Couldn't open the template file ($template). Please check and make sure that this file is where it's supposed to be.");
$output = implode("\n", $output);

#calculate number of questions in the database
$SqlTotalQuestions=mysql_query("select max(id) as total from question");
$RowTotalQuestions=mysql_fetch_array($SqlTotalQuestions);
$TotalQuestions=$RowTotalQuestions["total"];

#Pick a random question
$RandomQuestion=rand(1,$TotalQuestions);
$SqlQuestionSelected=mysql_query("select id, question from question where id=$RandomQuestion") 
	or die ("Error... Cant select a valid question");

#populate the random question
$RowQuestionSelected=mysql_fetch_array($SqlQuestionSelected);
$TheQuestion = htmlspecialchars($RowQuestionSelected["question"]);
$IdTheQuestion = $RowQuestionSelected["id"];

#select/populate choices array
$SqlA = "SELECT * from answer where answer.idq=$IdTheQuestion";
$regA=mysql_query($SqlA) or die(mysql_error());
$j=0;
while($Answers = mysql_fetch_array($regA) ) {
	$IdChoices[$j]=$Answers["id"];
	$Choices[$j]=$Answers["answer"];
	$j++;
}

#build form output for CHOICES
$nChoices=sizeof($Choices);
$choiceoutput="";
$Text="";

for($i=0; $i<$nChoices; $i++) {
	$Text='<input type="radio" name="answer" value="' . $IdChoices[$i] . '"';
	if($i==0) {
		$First=" CHECKED ";
		$Text.=$First;
	}
	$Text.=">  ". $Choices[$i] . "</input>";
	$choiceoutput.= $Text."<br />";
}
#close the database connection, we just dont need anymore
mysql_close();

$time = time();
$formoutput ="<input type=\"hidden\" name=\"question\" value=\"" . $TheQuestion . "\">";
$formoutput .="<input type=\"hidden\" name=\"idquestion\" value=\"" . $IdTheQuestion . "\">";
$formoutput .="<input type=\"hidden\" name=\"time\" value=\"" . $time . "\">";
$formoutput .= $choiceoutput;
$resultoutput=($_REQUEST["resultoutput"]=="") ? "" : $_REQUEST["resultoutput"];

$output = str_replace("%%RESULT%%", $resultoutput, $output);
$output = str_replace("%%NUMQUESTION%%", $_SESSION['questions_count'], $output);
$output = str_replace("%%QUESTION%%", $TheQuestion, $output);
$output = str_replace("%%FORM%%", $formoutput, $output);
echo $output;
exit();
?>
trivia1.php: File that judge the user's answer.
<?php
require("config.php"); 

if ($logged_in == 0) {
	die('Sorry you are not logged in, this area is restricted to registered members. <a href="login.php">Click here</a> to log in.');
}

#$progressive determines whether the user is given another
#random question after answering a question. If $progressive
#is set to 0, the script will only display the results from
#the previous question. If set to 1, it will display the
#results along with another question. Because questions are
#chosen in random order, it's possible that the same question
#will appear twice in a row during a progressive game.

$progressive = 1;
$result="";
$Points=1;
#Judge the user's answer, if one was submitted
if(isset($_POST["question"])) {
	if($_POST["question"]) {
		#Remove slashes that came through as submitted data
		$question=stripslashes($_POST["question"]);
		$idquestion=$_POST["idquestion"];
		if($_POST["answer"]) {
			#Find the if the answer submitted is the correct choice of the relevant question
			$SqlA = "SELECT * from answer where answer.idq=$idquestion and answer.id=" . $_POST["answer"];
			$regA=mysql_query($SqlA) or die(mysql_error());
			$RowA = mysql_fetch_array($regA);
			if ($RowA["correct"]==1) {
				# update user stats and display if its a correct answer
				$result="Correct!";
			}
			else {
				$result="Incorrect!";
				$Points=-1;
			}
		}
		$update_points = "UPDATE registration SET points=points+$Points WHERE nickname = '".$_SESSION['nickname']."'";
		mysql_query($update_points) or die(mysql_error());
		#close the database connection, we just dont need anymore
		mysql_close();
		#Should we show another question?
		if(!$progressive) exit();
	}
	else {
		$result = "";
	}
}
else {
		$result = "";
}
$Url2Go="trivia.php?resultoutput=$result";
Header("Location: $Url2Go");
exit();
?>
That's all the php code required, the following are the html templates files to run correctly the trivia timed test:

formtemplate.html: template file that include the script to control the time.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>::Trivia::</title>
</head>
<body bgcolor="#99CCCC">
<form name="form" method="POST" action="trivia1.php">
<h3 align="center"><font color="#993300">Trivia!</font></h3>
<table width="90%" border="0" align="center" bgcolor="#000000" cellpadding="0" cellspacing="1">
  <tr>
	<td>
	  <table width="100%" border="0" bgcolor="#FFFFFF">
		<tr> 
		  <td style="text-align:center;font-size:10pt;font-family:Verdana;font-weight:bold; text-decoration:underline">%%RESULT%%
		  </td>
		</tr>
		<tr> 
		  <td style="text-align:left;font-size:10pt;font-family:Verdana, Arial;">Question #%%NUMQUESTION%%: <span style="font-weight:bold;color:#FF0000;font-size:12pt">%%QUESTION%%</span>
		  </td>
		</tr>
		<tr> 
		  <td style="text-align:left;font-size:10pt;font-family:Verdana, Arial;">Which answer looks the best?
		  </td>
		</tr>
		<tr> 
		  <td style="text-align:left;font-size:10pt;font-family:Verdana, Arial;padding-left:20px">%%FORM%%
		  </td>
		</tr>
		<tr>
		<!-- countdown script --> 
			<td style="text-align:left;font-size:10pt;font-family:Verdana, Arial;">Time Left: <input type="text" name="seconds" size="3">
			<script language="javascript" type="text/javascript">
				var myTime = 20;
				function countDown() {
					document.form.seconds.value = myTime;
					if (myTime == 0) {
						location.href="trivia1.php";
					}
					else if (myTime > 0) { 
						myTime--;
						setTimeout("countDown()",1000);
					}
				}
				
				countDown();
			</script>
			</td>
		</tr>
		<tr> 
		  <td style="text-align:center"><input type="submit" value="Answer" name="submit">
		  </td>
		</tr>
	</table>
	</td>
  </tr>
</table>
</form>
<p align="center"><a href="logout.php"><font size="2" face="Verdana, Arial">Click here to Logout</font></a></p>
</body>
</html>
template4.html: Template file used in the login.php file if the user complete his number of tests per day.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>::Trivia::</title>
</head>
<body bgcolor="#99CCCC">
<h2 align="center"><font color="#FFFFFF">Trivia!</font></h2>
<table width="90%" border="0" align="center" bgcolor="#000000" cellpadding="0" cellspacing="1">
  <tr>
	<td>
	  <table width="100%" border="0" bgcolor="#131313">
		<tr> 
		  <td>
			<p><font size="2" face="Verdana, Arial">Sorry, but today you can not play our Trivia again, because you complete all your plays, please try again tomorrow.</p>
			<p><font size="2" face="Verdana, Arial">Thanks to play our Trivia %%NICKNAME%%. <a href="logout.php"><font size="2" face="Verdana, Arial">Click here to Logout</font></a>.</p>
		  </td>
		</tr>
	  </table>
	</td>
  </tr>
</table>
</body>
</html>
template5.html: Template file used in the trivia.php file if the user complete his number of tests per day.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>::Trivia::</title>
</head>
<body bgcolor="#99CCCC">
<h3 align="center"><font color="#993300">Trivia!</font></h3>
<table width="90%" border="0" align="center" bgcolor="#000000" cellpadding="0" cellspacing="1">
  <tr>
	<td>
	  <table width="100%" border="0" bgcolor="#ffffff">
		<tr> 
		  <td> 
			<p><font size="2" face="Verdana, Arial">Thanks to play our Trivia %%NICKNAME%%</p>
			<p><font size="2" face="Verdana, Arial">Sorry, but today you can not play our Trivia again, because you complete all your plays, please try again tomorrow.</p>
		  </td>
		</tr>
	  </table>
	</td>
  </tr>
</table>
<p align="center"><a href="logout.php"><font size="2" face="Verdana, Arial">Click here to Logout</font></a>.</p>
</body>
</html>
template3.html: Template file used in the trivia.php file if the user complete all the questions of the test.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>::Trivia::</title>
</head>
<body bgcolor="#99CCCC">
<h3 align="center"><font color="#993300">Trivia!</font></h3>
<table width="90%" border="0" align="center" bgcolor="#000000" cellpadding="0" cellspacing="1">
  <tr>
	<td>
	  <table width="100%" border="0" bgcolor="#FFFFFF">
		<tr> 
		  <td> 
			<p align="center"><font size="2" face="Verdana, Arial"><b><u>%%RESULT%%</u></b></font></p>
			<p><font size="2" face="Verdana, Arial">Thanks to play our Trivia %%NICKNAME%%</p>
			<p><font size="2" face="Verdana, Arial">You can play our Trivia %%PLAYSLEFT%% more time(s).</p>
		  </td>
		</tr>
	  </table>
	</td>
  </tr>
</table>
<p align="center"><a href="logout.php"><font size="2" face="Verdana, Arial">Click here to Logout</font></a> or <a href="trivia.php"><font size="2" face="Verdana, Arial">Click here to Play again</font></a>.</p>
</body>
</html>
Well, as you can see there are a lot of improvements to do and i must do :( when i have some time in the near future i hope, so please feel free to PM me if you have any question.

Best regards,

#19 Guest_iGuest-prasadh_*

Guest_iGuest-prasadh_*
  • Guests

Posted 08 September 2008 - 01:09 PM

How to make ane online examination to using php and msql? please send me all php script.

-question by prasadh



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users