Page 1 of 1

php quiz dilemma

Posted: Sun Apr 06, 2008 6:06 pm
by sm_558
Hi, i am having problems with the following code for a quiz. When the code is executed it prints out all the questions from the database along with the four answers related to it. Originally the form was outisde of the while loop but this would only let me select one radio button on the whole page. When i put the form into the while loop it allowed me to select an answer for each question however this resulted in multiple submit buttons.

Is there a way i can retrieve all questions from the database related to that quiz on one page?

Code: Select all

<?php
session_start(); 
// This script handle all the user commands.
require_once ("database.php");
require_once ("globals.php");
require_once ("loginlib.php");
$usertype = Getusertype ();
$username = Getusername ();
$userid = Getuserid ();
$title = $g_system_name . " -- Dashboard";
 
ShowHeader ($title);
ShowLinks (5);
 
?>
 
 
 
<?
 
//require_once ('mysql_connect.php'); // Connect to the db.
mysql_connect("localhost", "root", "") or die ("Unable to connect to the server");
// select the database or return an error message
mysql_select_db("nluser") or die ("Unable to connect to the database"); 
 
//get the id if the quiz that is to taken
$id = $_GET['id']; 
echo "Quiz ID:&nbsp;&nbsp;".$id; ?>
<br><br>
<?
 
 
//$query = "SELECT * FROM questions WHERE quizID='$id' ";
//$result=mysql_query($query);
//echo mysql_query($query);
$result = mysql_query("SELECT * FROM questions WHERE quizID='$id'"); 
 
//while($row = mysql_fetch_row($result))
//{
//echo $row['question_no'].",".$row['question']."<br/>";
 
 
    //sergeys
    
    //echo "query works<br>";
    $answers = array();
    $question="";
    $answerblock=array();
    $rightanswer=array();
 
    //echo "arrays work<br>";
    
    //{  
    //print_r($row);
    if(mysql_numrows($result))
    {
        while($rows = mysql_fetch_row($result))
        {
        
        print('<form name=myform method="post" action="quizresults.php">');
        //$rows = mysql_fetch_row($result);
        
            
            //print_r($row);
 
            //echo "start of the while loop - under the form<br>";
            $qid=$rows[0];
            $question=$rows[1];
            $answers[0]=$rows[3];
            $answers[1]=$rows[4];
            $answers[2]=$rows[5];
            $answers[3]=$rows[6];
 
            $question_no=$rows[2];
 
            $rightanswer=$rows[3];
 
            //echo "assign values to array<br>";
 
 
 
            srand((float) microtime() * 10000000);
 
            $rand_keys = array_rand($answers, 4);
 
 
            $answer0=$answers[$rand_keys[0]] ;
            $answer1=$answers[$rand_keys[1]] ;
            $answer2=$answers[$rand_keys[2]] ;
            $answer3=$answers[$rand_keys[3]] ;
            
            
 
            
 
                print('<tr><td><input type="radio" name=answer value="'.$answer0 .'">&nbsp;&nbsp;&nbsp;'.$answer0.'</td></tr>');
 
                print('<tr><td><input type="radio" name=answer value="'.$answer1 .'">&nbsp&nbsp&nbsp'.$answer1 .'</td></tr>');
 
                print('<tr><td><input type="radio" name=answer value="'.$answer2 .'">&nbsp&nbsp&nbsp'.$answer2 .'</td></tr>');
 
                print('<tr><td><input type="radio" name=answer value="'.$answer3 .'">&nbsp&nbsp&nbsp'.$answer3 .'</td></tr><br>');
            
                print('<tr><td>&nbsp<input type=hidden name=rightanswer value="'.$rightanswer .'"></tr>');
            
            print('<tr><td colspan=2><input type=submit name=submit value="Submit" >
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            <input type=reset name=reset value="reset" ></td></tr></table>');
            
            
            print('</form>');
            
            }
                //print('<tr><td>&nbsp<input type=hidden name=rightanswer value="'.$rightanswer .'"><input type= hidden name=qnumber value="'.$qnumber .'"></tr>');
        }
        
 
 
?>
</form>
<?
ShowFooter();
?>
scottayy| Please use [ code ] and [ code=php ] BBCode tags when posting code in the forums.

Re: php quiz dilemma

Posted: Mon Apr 07, 2008 10:19 am
by andym01480
So many questions! And a bit confusing too.

Retrieving stuff from the database will happen if you uncomment the query
To only have the one submit button, put the form line and the submit button line and end form line outside of the loop.

Didn't understand the other question, but I hope that helps