Jump to content
Sign in to follow this  

Help with Create Exam webpage

Recommended Posts

<!doctype html>
<meta charset="UTF-8">
<title>Exam Database - Create Exam</title>
<link href="mainstyle.css" rel="stylesheet" type="text/css">

<?php include "title.php"; include "sql-connect.php"; ?>

<div id="content">


$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';	//checks the page lifetime

//set the session var to empty
if($pageWasRefreshed) {
	$_SESSION = []; //manually destroy session data
	session_destroy(); //destroy session data

//test if session vars are set and convert to local var
if(isset($_SESSION['s_id']) && !empty($_SESSION['s_id'])) {
	sort($_SESSION['s_id']);		//echo 'session var s_id set';
	$subids = array_unique($_SESSION['s_id']);	//make array unique

//check if message or error exists and if so then output to page
if((isset($_SESSION['exammsg']) && !empty($_SESSION['exammsg'])) || isset($_SESSION['examerror']) && !empty($_SESSION['examerror'])) {
	//one or both may exist so use if statement to check
	if(isset($_SESSION['exammsg'])) {$exammsg = $_SESSION['exammsg'];}
	if(isset($_SESSION['examerror'])) {$examerror = $_SESSION['examerror'];}
	//echo out a container
	echo '<div class="msg-cont"><p class="msg-head">Message:</p>';
		if(isset($exammsg)) {
			echo $exammsg.'<br />'; //if messages exist then output to page
		if(isset($examerror)) {
			echo $examerror.'<br />'; //if errors exists then output to page
	echo '</div>'; //close container

//test if questions exist
if(isset($_SESSION['q_text']) && !empty($_SESSION['q_text'])) {
	sort($_SESSION['q_text']);	//sort the array
	$questions = $_SESSION['q_text'];	//set questions session var to local var

echo '<form action="scripts/create/addsub.php" method="post">
	<fieldset>';	//start by echoing the form and fieldset

//query the number of subjects in the subject_tbl
$q_subs = sqlsrv_query($connect_exam, "SELECT s_id, s_text FROM Subject") or die(sqlsrv_errors($connect_users));

//create local variable array called subjects
while($subject = sqlsrv_fetch_array($q_subs)) {
	$subjects[] = $subject;		//put the subject var into an array called subjects

echo '<select name="subid">';	//echo a select input

if(isset($subjects) && !empty($subjects)) {	
	echo '<option disabled selected>Select a Subject</option>';		//this is our default disabled option
	foreach($subjects as $sub) {	//for each subject in the subjects array
		if(isset($subids)) {
			if(in_array($sub['s_id'], $subids)) {
				//if it already exists in the subids array echo a disabled version
				echo '<option value="'.$sub['s_id'].'" disabled>'.$sub['s_text'].'</option>';
			} else {
				//echo the subject
				echo '<option value="'.$sub['s_id'].'">'.$sub['s_text'].'</option>';
		} else {
			//echo the subject
			echo '<option value="'.$sub['s_id'].'">'.$sub['s_text'].'</option>';
} else {
	//no subjects exist so none available to select
	echo '<option disabled selected>No Subjects</option>';
echo '</select>';	//close select input

echo '<input type="submit" name="addsub" value="Add Subject" />';	//echo submit button
echo '</fieldset></form>';	//close fieldset and form elements

//check if subjects var has subjects in it
if(isset($subids) && !empty($subids)) {

	//echo our table header row
	echo '<table border="1" cellspacing="6px" cellpadding="6px">
			<tr class="header-row">
    			<th>Subject Name</th>
				<th>Questions in Subject</th>
        		<th>Essential Qs Required</th>
				<th>Non-Essential Qs Required</th>
				<th>Add Questions</th>
				<th>Remove Subject</th>
	foreach($subids as $subid) {	//foreach statement to echo form for adding questions for each subject
		//query for subject name
		$qsubnames = sqlsrv_query($connect_exam, "SELECT s_id,s_name 
			FROM subject 
			WHERE s_id='".$subid."'") or die(sqlsrv_errors($connect_exam));
			//while the current query is at X row do this
			while($row = sqlsrv_fetch_array($qsubnames)) {
				echo '<tr>

				//query for number of non-essential-questions a subject has
				$qnon_ess_qs = sqlsrv_query($connect_exam, "SELECT * 
					FROM question
					WHERE s_id='".$subid."'
					AND essential_flag='0'") or die(sqlsrv_errors($connect_exam));
				//sets the number of rows returned from the query to a var
				$nonessqs = sqlsrv_num_rows($qnon_ess_qs);

				//query for number of non-essential-questions a subject has
				$q_ess_qs = sqlsrv_query($connect_exam, "SELECT *                         (ASK DAVE ABOUT THIS QUERY)
					FROM question 
					WHERE s_id='".$subid."'
					AND essential_flag='1'") or die(sqlsrv_errors($connect_exam));

				//sets the number of rows returned from the query to a var
				$essqs = sqlsrv_num_rows($q_ess_qs);

				$total_qs = $essqs + $nonessqs;

				//echos a form for adding questions and removing them
				echo '<td>'.$total_qs.'</td>
				<form action="scripts/create/addqs.php" method="post">
					<td><input type="number" name="noness_qs" min="1" max="'.$nonessqs.'" value="1" /></td>';
					if($essqs > 0) {
						echo '<td><input type="number" name="ess_qs" min="1" max="'.$essqs.'" value="1" /></td>';
					} else {
						echo '<td>No Essential In Subject!</td>';
			  echo '<td>
						<input type="hidden" name="subid" value="'.$row['s_id'].'" />
						<input type="submit" name="addqs" value="Add Questions" />

				<form onsubmit="';
					echo "return confirm('Any Questions selected will be removed from the table. Are you sure?');";
					echo '" action="scripts/create/remsubs.php" method="post">
					<td><input type="hidden" name="delsub" value="'.$row['s_id'].'" />
						<input type="submit" name="remsubs" value="Remove Subject" />

	echo '</table>';	//close table
} else {
	echo '<p>Please add subjects to the exam you want to create.</p>';	//No subjects have been added yet

//check if questions array is set and not empty
if(isset($questions) && !empty($questions)) {	
	echo '<br />
	<h2>Below is the list of questions randomly selected from the subject you chose</h2>
	<br />
	<table border="1" cellpadding="5px" cellspacing="5px">
		<tr class="header-row">
			<th>Question Id</th>
			<th>From Subject</th>
			<th>Times Used</th>
			<th>Correct Answers</th>
			<th>Remove Question?</th>
	//foreach question in the questions array echo...
	foreach($questions as $question) {
		echo '<tr>
				<form action="scripts/create/remqs.php" method="post">                  
					<input name="remqs" type="submit" value="Remove Question" />
					<input name="remqid" type="hidden" value="'.$question['q_id'].'" />
	echo '</table>';	//close the table
	echo '<br />';
	//form to create an exam if questions have been added successfully
	echo '<form action="scripts/create/addexam.php" method="post">
		  	<fieldset class="btn-cont">
		  		<input name="addexam" type="submit" value="Create Exam" />





I am new to all this and currently developing a exam database with a web front end. The web front end will enable the user to enter what subjects he would like in the exam, how many questions on it (each exam must have at least 10 essential questions on it, and it will produce a exam for him/her.

The page I am having trouble with is the create exam page. The basic design of the page is to have a table which consists of Subject name, questions in subject, essential questions required, non essential questions required, add question option and remove subject option. The user then edits the options in the table and an exam is created on the bottom of the page. Before I go into the code I will briefly describe my database (MS Sql server). So I have a Answer table (a_id(pk),q_id(fk),a_text,a_correct. A Exam table (e_id(pk), e_date, e_duration. An Examentry table (q_id(pk,fk),e_id). A Subject table (s_id(pk),s_text). And also a Question table (q_id(pk),s_id(fk), q_text, q_essential, q_disabled.


I have added the code for my webpage on this post. I appreciate that it is a long read and I have tried my best to solve the issues before posting on a forum, but any help would be much appreciated.


Thank you for your time.





Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing

    No registered users viewing this page.

  • Member Statistics

    • Total Members
    • Most Online

    Newest Member
  • Forum Statistics

    • Total Topics
    • Total Posts