OK... I'ts been so long since I asked for help and yet Im still once again here asking for another...
There is this case of mine, a quiz program, wherein questions will come from a certain table, which is called by certain explode functions and foreach statements. But my problem now is what will I do to save all the answers to the database? Each input box or textarea that emerges from the explode statement is set to a single name. (which I had added [] to my luck that it will support it)... BTW the following code is UP with the help of one fellow around here, wait ill search for his name....
CODE
<form method="GET" action="submitexam.php" onSubmit="if(!confirm('Are you sure you wanted to submit your E-TEST Paper?'))return false;">
<font face="Verdana, Arial, Helvetica" size="-2">
<?php
if(isset($_GET['ExamID']) && isset($_GET['Subject'])) {
if($_GET['Subject'] == 'VB') {
$database = 'vbexambank';
}
elseif($_GET['Subject'] == 'CPP') {
$database = 'cppexambank';
}
elseif($_GET['Subject'] == 'SQL') {
$database = 'sqlexambank';
}
elseif($_GET['Subject'] == 'JAVA') {
$database = 'javaexambank';
}
else {
echo "Invalid Exam ID...";
}
$IDe = $_GET['ExamID'];
$queryfirst = "Select ExamContent from schedexam WHERE ID = '$IDe'";
$resultset = mysql_query($queryfirst) or die('Unable to Connect to the Exam Database. Please report this to the administrator.');
$row = mysql_fetch_row($resultset);
$QuestionSet = $row[0];
echo "<font face=\"tahoma\" size=4><b>Do not add any unecessary tabs, spaces, or unwanted symbols<BR>
with your answers because this might lead your answers to be wrong.</b></font><BR><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=4>
<strong>Also, AVOID hitting the ENTER KEY unless you're in the debugging type of exam.<br>
TO AVOID Early SUBMISSION of E-TEST PAPER..</strong><bR>
</font>";
$sections = explode(',',$QuestionSet);
echo "<table>";
foreach($sections as $idx => $val){
$ids = explode(":",$val);
$QuestionItems[$ids[0]] = explode('-',$ids[1]);
}
foreach($QuestionItems as $idx => $val) {
if($idx == 1) {
$titleexam = '<tr><Td><B><font face=tahoma size=3>Identification</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Identification</font><br>';
}
elseif($idx == 2) {
$titleexam = '<tr><Td><B><font face=tahoma size=3>Modified True or False</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Modified True or False</font><br>';
}
elseif($idx == 3) {
$titleexam = '<tr><Td><B><font face=tahoma size=3>Debugging</font></b><br></td></tr>';//echo '<font face=tahoma size=3>Debugging</font><br>';
};
echo $titleexam;
shuffle($val);
foreach($val as $id => $value) {
$query = "Select QuestionType, Question, ID from ".$database." WHERE ID = '".$value."'";
$result = mysql_query($query) or die('There was an error fetching the questions from the database. Please try again. Please report to the administrator if this error still exist.');
$row = mysql_fetch_row($result);
echo "<tr><td><li></td><td>"; ?>
<?php
if ($row[0] == 'DEBUGGING') {
echo "<textarea rows=10 cols=50 name=ExamContent[".$row[2]."]>$row[1]</textarea><input type=hidden value=$row[2] name=ID[$row[2]]></td>";
} else {
echo $row[1];
} ?>
<tD>
<?php
if ($row[0] == 'DEBUGGING') {
echo "<textarea rows=10 cols=50 name=ExamContent[".$row[2]."]></textarea><input type=hidden value=$row[2] name=ID>";
} elseif($row[0] == 'TRUEORFALSE') {
echo "<select name=ExamContent[".$row[2]."]>
<option selected >------</option>
<option value=TRUE>True</option>
<option value=FALSE>False</option>
</select><input type=hidden value=$row[2] name=ID[$row[2]]><input type=text name=Answer[".$row[2]."] size=20>";
}
else {
echo "<input type=text name=ExamContent[".$row[2]."] size=50><input type=hidden value=$row[2] name=ID[$row[2]]> ";
} ?>
</td><p>
<?php }
}
echo "<input type=hidden value=$_GET[Subject] name=Subject>";
echo "</tr><TR><TD><input type=\"submit\" name=\"Submit\" id=\"submit\" value=\"Submit\"></TD></TR></table>";
} // close if statement isset
else
{
echo "<font size=4><B>You have not selected an exam properly! Click back and select an exam!</B></font>";
}
?>
Now I made this code of mine to call each of entries of each textbox but my problem now is the index of the Array I used... and another fact that every RELOAD / REFRESH of the page randomizes the items makes me more wonder how...
CODE
<?php
session_start();
require_once('db.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
if(isset($_GET['Submit'])) {
foreach($_GET['ExamContent'] as $value) {
echo $value.'<BR>';
$searchValue = $value;
$key = array_search($searchValue, $_GET['ExamContent']);
echo $key.'<BR>';
}
}
$array = array("apple", "banana", "cherry"); // DUMMY
$searchValue = "banana";
$key = array_search($searchValue, $array);
echo $key;
?>
As you can see, I made some dummy data about searching for each Index Key of the Array from the List of Items Submitted but, whenever it encounters entries that are the same, LIKE the TRUE OR FALSE section, (of course the TRUE or False Section can have items that have similar items) it returns same keys which is not I expected....
Sorry if I had a hard time writing this thread of mine and how bad my English is.. but if ever someone knows how to deal with this, please?
OK I repeat for more clarification... *Laughs*
I have the first code above to call questions from some table in the database. When I press the submit button, the script file should evaluate each input the user has to give and store each on the database together with his/her user ID...
PS:
The Random Thingy is an essential feature... to minimize the you-know-what thingy
This post has been edited by aco99: 9 Oct, 2008 - 12:49 PM