Page 1 of 1

testing app.

Posted: Wed Aug 22, 2007 7:34 pm
by tecktalkcm0391
Hello, I am developing a app for school which you can create a practice test and use the Test ID to access your test, and I need help displaying the actually testing part. I want it so that the script pics a random question (one that hasn't been used before, and makes you answer that question (no refreshing to change the question) if your right it counts it correct, same for wrong. Whats below is what I have so far. All I need help with at this point is making the question "stay" the current question... any critiquing would be awsome!

Note: this isn't for actually testing, just for practice test so it doesn't have to be "cheat proof", but that would be nice :)

Code: Select all

<?php
ob_start();
@require("db.php"); //database settings... i'm using @ because i'm lazy and dont want any error print outs.
session_name('eTests');
session_start();

if(!isset($_GET['testID']) && !is_numeric($_GET['testID']) && !isset($_POST['testID']) && !is_numeric($_POST['testID'])){
	header("Location: index.php");
	exit();
} else {
	if(!isset($_SESSION['questionUsed'])){
		$_SESSION['questionUsed'] = array();
	}
	
	$testID = $_GET['testID'];
	$getTestInfo = mysql_query("SELECT * FROM `tests` WHERE `testID` =".$testID." LIMIT 1");
	$getTestInfoArray = mysql_fetch_array($getTestInfo);
	$testName = $getTestInfoArray['testName'];
	
	$getQuestions = mysql_query("SELECT * FROM `questions` WHERE `testID` =".$testID."");
	while($row = mysql_fetch_array($getQuestions)){
		$questions[$row[0]] = $row;
		$questionsAll[] = $row[0];
	}
	$qCount = count($questions);
	if(isset($_POST['questionID']) && !empty($_POST['questionID'])){
		if(!in_array($questionID,$_SESSION['questionUsed'])){
			$_SESSION['questionUsed'][] = $questionID;
		}
	}
	
	foreach($questionsAll as $cQ){
		if(!in_array($cQ,$_SESSION['questionUsed'])){
			$questionToUse[] = $cQ;
		}
	}
	if(count($questionToUse)!=0){
		
		foreach($questions as $thisQ){
			if(in_array($thisQ[0],$questionToUse)){
				$questionList[] = $thisQ;
			}
		}
		srand((float)microtime() * 1000000);
		shuffle($questionList);
		
		$theQuestion = $questionList[0];
		$currentQuestion = $theQuestion['testQuestion'];
		$currentQuestionID = $theQuestion['questionID'];
		print_r($theQuestion);
	} else {
		$completed = true;
		unset($_SESSION['questionUsed']);
	}
	
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $testName; ?></title>
<style type="text/css">
<!--
.Title {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 24px;
	font-weight: bold;
}
-->
</style></head>

<body>
<div align="center"><span class="Title"><?php echo $testName; ?></span></div>
<p>&nbsp;</p>
<form id="question" name="question" method="post" action="?testID=<?php echo $testID; ?>">
  <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><strong>
        <input name="questionID" type="hidden" id="questionID" value="<?php echo $currentQuestionID; ?>" />
        <input name="testID" type="hidden" id="testID" value="<?php echo $testID; ?>" />
     <?php if($completed==false){ ?> Q:&nbsp;&nbsp;<?php echo $currentQuestion; ?><?php } else { echo '<center>You have completed the test.</center>'; }?></strong></td>
    </tr>
    <tr>
      <td><?php 
	  if($currentQuestion){
	  ?>
        <label for="textfield"><strong>A:&nbsp;</strong></label>
        <input name="answer" type="text" id="answer" size="50" maxlength="255" />
	  <?php
	  }
	  ?>&nbsp;</td>
    </tr>
    <tr>
      <td align="center" valign="middle" style="padding-top:10px;"><label for="Submit"></label>
      <input type="submit" name="Submit" value="Submit" id="Submit" /></td>
    </tr>
  </table>
</form>
<p>&nbsp;</p>
</body>
</html>
<?php } ?>

Posted: Wed Aug 22, 2007 9:30 pm
by Zoxive

Code: Select all

@require("db.php"); //database settings... i'm using @ because i'm lazy and dont want any error print outs. 
??

include vs require

Posted: Wed Aug 22, 2007 9:40 pm
by tecktalkcm0391
Zoxive wrote:

Code: Select all

@require("db.php"); //database settings... i'm using @ because i'm lazy and dont want any error print outs. 
??

include vs require
require has error messages that are printed out if the database doesn't connect so i'm just using the @ to shut it up if it says something for now. i'll fix that one the actual codes are done.

Posted: Thu Aug 23, 2007 10:11 pm
by tecktalkcm0391
can anyone help me with keeping the "current" question CURRENT

Posted: Thu Aug 23, 2007 10:39 pm
by John Cartwright
A little bored tonight, take a look at this example

Code: Select all

/** 
 * Start the session
*/
session_start();
if (!isset($_SESSION['answered'])) {
   $_SESSION['answered'] = array();
}

/**
 * Sanity check, make sure we have neccesary data
*/
if (!empty($_POST['answer']) && isset($_SESSION['currentQuestionId'])) {
   /**
    * However you update your database..
   */
   $sql = 'UPDATE answers SET answer = \''. mysql_real_escape_string($_POST['answer']) .'\', question_id = '. intval($_SESSION['currentQuestionId']);
   mysql_query($sql) or die(mysql_error());

   /**
    * Store then remove the current question id 
   */
   $_SESSION['answered'][] = $_SESSION['currentQuestionId'];
   unset($_SESSION['currentQuestionId']);
}

/**
 * This is to avoid the refreshing problem, if we have an active question fetch it
*/
if (isset($_SESSION['currentQuestionId'])) {
   $sql = 'SELECT * FROM questions WHERE id = '. intval($_SESSION['currentQuestionId']);   
}
/**
 * We don't want to fetch answered questions..
*/
else {
   $sql = 'SELECT * FROM questions WHERE id NOT IN ('. implode(', ', $_SESSION['answered']).') ORDER BY rand() LIMIT 1';   
}

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

/** 
 * Check if we have any more questions left
*/
if (mysql_num_rows($result)) { 
   $question = mysql_fetch_assoc($result);
   $_SESSION['currentQuestionId'] = $question['id'];
}
else {
   //no questions left
}

Posted: Thu Aug 23, 2007 10:40 pm
by Christopher
I would recommend using the session for this. Initialize the random question order by shuffling an array. Maintain the current question number and the answers in the session as well. Using an object for this and saving it in the session will simplify keeping everything together.