Jump to content

PHP Quiz - Please Help!!


akramm

Recommended Posts

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?

 

 

Line number On/Off | Expand/Contract

<?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:  ".$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 .'">   '.$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();

?>

 

Link to comment
Share on other sites

Only the questions should be inside the while loop, since those are the only things you wish to loop though. The form opening/closing tags and the submit button should be outside the loop.

 

When you post your code, could you place it inside


tags please. If you could also use full opening tags (<?php) it would be a great help - we'll then get syntax highlighting and it'll be much easier to read your code and help you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.