akramm Posted April 7, 2008 Share Posted April 7, 2008 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 .'">   '.$answer1 .'</td></tr>'); print('<tr><td><input type="radio" name=answer value="'.$answer2 .'">   '.$answer2 .'</td></tr>'); print('<tr><td><input type="radio" name=answer value="'.$answer3 .'">   '.$answer3 .'</td></tr><br>'); print('<tr><td> <input type=hidden name=rightanswer value="'.$rightanswer .'"></tr>'); print('<tr><td colspan=2><input type=submit name=submit value="Submit" >                             <input type=reset name=reset value="reset" ></td></tr></table>'); print('</form>'); } //print('<tr><td> <input type=hidden name=rightanswer value="'.$rightanswer .'"><input type= hidden name=qnumber value="'.$qnumber .'"></tr>'); } ?> </form> <? ShowFooter(); ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 7, 2008 Share Posted April 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
akramm Posted April 7, 2008 Author Share Posted April 7, 2008 Thanks Alot.. It worked... but I can only select one answer for all questions..... how do I get round that? Thanks!! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 7, 2008 Share Posted April 7, 2008 You need to be giving your radioboxes that apply to different questions different names. Using an array with the question ID as the key would be a good choice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.