New Posts  All Forums:Forum Nav:

Quiz Maker

post #1 of 5
Thread Starter 
hi..im new here..i was introduced by a member here to get help from this forum about my problem..
i need some hints to get my code to display scores of my quiz
what i want to do is getting questions from database (mysql) then scores will be displayed after answered by user

my database

Code:

CREATE TABLE question 
( q_id varchar(25) NOT NULL default '',
ques varchar(255) NOT NULL default '',
one varchar(200) NOT NULL default '',
two varchar(200) NOT NULL default '',
three varchar(200),
four varchar(200),
ans int(2) NOT NULL ) TYPE=MyISAM; 

# # Dumping data for table `question` # 
INSERT INTO question VALUES ('Programmer', 'PHP stands for what?', 'Personal Home Page', 'PHP: Hypertext Processor', 'Peter Holiday Planner', 'Positively Hot Programming', 2); 
INSERT INTO question VALUES ('Programmer','To print text to the browser, which command is used?','echo()','write()','text()','text2browser()',1);


calling question

Code:
if($subcat=$_POST['subcat']) //this $subcat here refer to category of question use choose
{
$ques=mysql_query("SELECT ques, one, two, three, four, ans FROM question where q_id='$subcat'");
$num=mysql_numrows($ques);
//$question = fetch_object($ques);
for($i=0; $i<$num; $i++)
{
$f1=mysql_result($ques,$i,"ques");
$f2=mysql_result($ques,$i,"one");
$f3=mysql_result($ques,$i,"two");
$f4=mysql_result($ques,$i,"three");
$f5=mysql_result($ques,$i,"four");
$f6=mysql_result($ques,$i,"ans");

$questions[] = array("$f1", "$f2","$f3","$f4","$f5",$f6);
}
include_once("score.php");

}


scoring (score.php)

Code:
if (isset($_POST['sent'])) 
{

for ($i=0;$i<count($questions);$i++) 
        {

  if ($_POST['q'.$i]=="c") 
                {
  $score++;
  }
}
$percent = number_format(($score/count($questions))*100,1,".",",");
echo("<center><br><b>Your Score: </b><br \\><br \\><br \\><b><font size='10'>".$percent."%<font size></b><br><br></center>\
");
} 
else 
{
echo("<form action=\\"#\\" method=\\"post\\">\
");
//echo("<input type=\\"hidden\\" name=\\"sent\\">\
");
for ($i=0;$i<count($questions);$i++) 
{
  echo("<b>".$questions[$i][0]."</b><br><br>\
");
  if ($questions[$i][5]==1) {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"c\\"> ".$questions[$i][1]."<br>\
");
  } else {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"w\\"> ".$questions[$i][1]."<br>\
");
  }
  if ($questions[$i][5]==2) {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"c\\"> ".$questions[$i][2]."<br>\
");
  } else {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"w\\"> ".$questions[$i][2]."<br>\
");
  }
  if ($questions[$i][5]==3) {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"c\\"> ".$questions[$i][3]."<br>\
");
  } else {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"w\\"> ".$questions[$i][3]."<br>\
");
  }
  if ($questions[$i][5]==4) {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"c\\"> ".$questions[$i][4]."<br><br>\
");
  } else {
  echo("<input type=\\"radio\\" name=\\"q".$i."\\" value=\\"w\\"> ".$questions[$i][4]."<br><br>\
");
  }
}

echo("<br \\><input type=\\"submit\\" value=\\"Score\\">");
}


i've managed to display all the questions..only prob is scoring part..
thanks in advance for u help!
post #2 of 5
Thread Starter 
mmmi guess it's really impossible to fix huh?
then any other suggestion to what other methods can i do this?
post #3 of 5
I very very briefly looked at your code, but two things popped out when overviewing it...

1 - echo("<form action=\\"#\\" method=\\"post\\">\
"); - You're not sending your form anywhere, you should be calling score.php again (action="score.php")

2 - //echo("<input type=\\"hidden\\" name=\\"sent\\">\
"); - You have your trigger commented out, so even if you called score.php the hidden variable of 'sent' is always unset never triggering your scoring mechanism
post #4 of 5
Thread Starter 
hi! thanks for replying
the echo i forgot to change the comment because before i was trying $_POST['submit']

for your first suggestion i tried but received

"Warning: Division by zero in C:\\xampplite\\htdocs\\jobs\\quiz_maker.php on line 11", the line where the scoring calculation is..

i tested around and i found out at the calling question code if i do like this:

Code:
$questions[] = array("fsdfsd", "asfdf","fdasf","fsdf","sdgsdg",2); *// column 6 is the answer
$questions[] = array("fsdfsd", "asfdf","fdasf","fsdf","sdgsdg",3);
$questions[] = array("fsdfsd", "asfdf","fdasf","fsdf","sdgsdg",5);
include_once("score.php");
it will work but i want to call it from database so this method is not suitable

Code:

$ques=mysql_query("SELECT ques, one, two, three, four, ans FROM question where q_id='$subcat'");
$num=mysql_numrows($ques);

for($i=0; $i<$num; $i++)
{
$f1=mysql_result($ques,$i,"ques");
$f2=mysql_result($ques,$i,"one");
$f3=mysql_result($ques,$i,"two");
$f4=mysql_result($ques,$i,"three");
$f5=mysql_result($ques,$i,"four");
$f6=mysql_result($ques,$i,"ans");

}
$questions[] = array("$f1", "$f2","$f3","$f4","$f5",$f6); <-- move this out of the for loop
include_once("score.php");
this method also work but then only the last question will be assign into the array

so i guess not the score.php is the problem here but somewhere in how i call the question put where i should put the include_once() function?
Edited by kurz - 3/7/11 at 8:15pm
post #5 of 5
Thread Starter 
can anyone suggest me a forum where it is really intended for programming?
im stuck with this problem for so long already that i feel like dropping it.
wanna give the last shot in a programming dedicated forum.

anyone knows? please
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding