multiple chocie coding??

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
suziecorbett
Forum Newbie
Posts: 3
Joined: Wed Nov 12, 2003 5:59 am
Location: dublin-ireland

multiple chocie coding??

Post by suziecorbett »

hey everybody. I am currently doing a web project using php and mysql. i have never used these languages before so I'm quite lost. Basically the code i am working on at the moment is a multiple choice exam. the code for marking it is as follows;

<?php
$_SCORE=0;

if($_POST["r1"]=='red'){
$_SCORE++;
}
if($_POST["r2"]=='red'){
$_SCORE++;
}

if($_POST["r3"]=='red'){
$_SCORE++;
}

if($_POST["r4"]=='red'){
$_SCORE++;
}

if($_POST["r5"]=='red'){
$_SCORE++;
}

if($_POST["r6"]=='red'){
$_SCORE++;
}

if($_POST["r7"]=='red'){
$_SCORE++;
}

if($_POST["r8"]=='red'){
$_SCORE++;
}

if($_POST["r9"]=='red'){
$_SCORE++;
}

if($_POST["r10"]=='red'){
$_SCORE++;
}


if($_SCORE==0){
print("Sorry you got them all wrong!");
}
else if($_SCORE==10){
print("Congratulations you got them all right!!");
}
else if ($_SCORE<4){

print (" Sorry you failed!");
?>

<br/>

<?php
print("Your score is: ");

print($_SCORE);
}


else {

print("Your score is: ");
print($_SCORE);
?>
<br/>

<?php
print (" congratulations you passed!");
}


?>

<html>
<head>
<body>

<p><a href="http://www.registerhome.com/~schubert/test3.html"><font color="#0000FF"><u>Take
the test again.</u></font></a></p>


</html>
</head>
</body>



i have the html code in a seperate file. my problem is that i want to ensure that users can only sit the exam at max 3 times before they have to resit the tutorial that leads up to it, any ideas??
Sorry if i haven't made my problem very clear, but any ideas ye have would be greatly appreciated!!
Thanks,
Suzie
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you can set a cookie when they start a exam, when thy do it the 2nd time, update cookie, do on the 3rd one. something like this (also checks if the user has done 3 or not)

Code: Select all

<?php
//this part sets cookie if not set already, if it is then updates the cookie.
if(!isset($_COOKIE['taken']))
{
$time  = 60*60*24//1 day
setcookie("taken", "1", time()+$time);
}
else
{
$value = $_COOKIE['taken'] + 1;
setcookie("taken", "$value", time()+$time);
}
?>
now, to check if the user has taken the exam 3 times or not:

Code: Select all

<?php
if($_COOKE['taken'] == 3)
{
die("Sorry, you already taken the exam 3 times.");
exit;
}
?>
this script can give you the idea, it wont prolyl work,i am really tired atm, just got back from full day at college 8O

you can go a step futher by usering a database (can be a text file), where you record the user ip and number of times they taken the test.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

The simplist way to do this would be to create a login system, then whenever they sit the test, add 1 to the total number of times they have sat it. If the number is 3 then you can make them resit the tutorial.
Post Reply