php - Getting radio button value in while loop -


i'm doing simple examination program using php.

here code taking quiz, it's still in testing don't mind design.

this session person taking quiz (no problem here):

<?php session_start (); $_session['username']; ?> <div id="wrap">     <?php     $host="localhost"; // host name      $username="root"; // mysql username      $password=""; // mysql password      $db_name="myproject"; // database name      $tbl_name="q_quiz"; // table name       // connect server , select databse.     mysql_connect("$host", "$username", "$password")or die("cannot connect");     mysql_select_db("$db_name")or die("cannot select db");      // value of id sent address bar      $id=$_get['q_id'];     $sql="select * $tbl_name q_id='$id'";     $result=mysql_query($sql);     $rows=mysql_fetch_array($result);     ?>      <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc">         <tr>             <td>                 <table width="100%" frame="box" cellpadding="3" cellspacing="1" bgcolor="#ccffcc">                     <tr>                         <td bgcolor="#ccffcc"><h3>quiz info : </h3></td>                     </tr>                     <tr>                         <td bgcolor="#ccffcc"><strong>quiz name : </strong><?php echo $rows['q_name']; ?></td>                     </tr>                 </table>             </td>         </tr>     </table>      <font size="6">     <div style="font-weight: bold" id="quiz-time-left"></div>      <script type="text/javascript">     var max_time = <?php echo $rows['q_time'] ?>;     var c_seconds  = 0;     var total_seconds =60*max_time;     max_time = parseint(total_seconds/60);     c_seconds = parseint(total_seconds%60);     document.getelementbyid("quiz-time-left").innerhtml='time left: ' + max_time + ' minutes ' + c_seconds + ' seconds';     function init(){         document.getelementbyid("quiz-time-left").innerhtml='time left: ' + max_time + ' minutes ' + c_seconds + ' seconds';         settimeout("checktime()",999);     }     function checktime(){         document.getelementbyid("quiz-time-left").innerhtml='time left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;         if(total_seconds <=0){             settimeout('document.quiz.submit()',1);         } else {             total_seconds = total_seconds -1;             max_time = parseint(total_seconds/60);             c_seconds = parseint(total_seconds%60);             settimeout("checktime()",999);         }     }     init();     </script>     </font>      <form action="result.php" method="post">     <?php     $score = 0;     $tbl_name2="a_quiz"; // switch table "forum_answer"     $sql2="select * $tbl_name2 q_id='$id'";     $result2=mysql_query($sql2);     while($rows=mysql_fetch_array($result2)){         $q_question = $rows['q_question'];         ?>         <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc">             <tr>                 <td>                     <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff">                         <tr style='overflow:hidden; word-wrap:break-word;'>                             <input type="text" name="q_id" value="<?php echo $rows['q_id'];?>">                             <td bgcolor="lightgreen"><strong>question:</strong></td>                             <td bgcolor="lightgreen">:</td>                             <td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question']; ?></td>                         </tr>                         <tr>                             <td bgcolor="lightgreen"><strong>a</strong></td>                             <td bgcolor="lightgreen">:</td>                             <td bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="a"><?php echo $rows['a'] ?></td>                         </tr>                         <tr>                             <td width="18%" bgcolor="lightgreen"><strong>b</strong></td>                             <td width="5%" bgcolor="lightgreen">:</td>                             <td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="b"><?php echo $rows['b'] ?></td>                         </tr>                         <tr>                             <td width="18%" bgcolor="lightgreen"><strong>c</strong></td>                             <td width="5%" bgcolor="lightgreen">:</td>                             <td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="c"><?php echo $rows['c'] ?></td>                         </tr>                         <tr>                             <td bgcolor="lightgreen"><strong>d</strong></td>                             <td bgcolor="lightgreen">:</td>                             <td bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="d"><?php echo $rows['d'] ?></td>                         </tr>                     </table>                 </td>             </tr>         </table>         <?php     }      $connection=mysql_connect('localhost', 'root','');     mysql_select_db('thesis');      $username= $_session['username'];      $query6 = mysql_query("select * student username='$username'");     $row6 = mysql_fetch_assoc($query6);     $s_id = $row6['id'];     $name = $row6['name'];     $email = $row6['email'];     $position = $row6['position'];     ?>     <input type="text" name="s_id" value="<?php echo $s_id ?>">     <input type="submit" name="submit" value="submit answer" class="btn">     </form> </div> 

i provided image (i inputted 2 questions, output of code above):

http://i60.tinypic.com/2lu8doh.jpg

(2 quiz id, no problem on this, 12 near submit button examiner id)

there's no problem in code above.

my problem starts here. getting answer of (examiner/person taking quiz) of question number 1 , found error.

this code below initiates when click submit button:

if(isset($_post['submit'])) {     $con = mysql_connect ("localhost","root","");     if (!$con)     {         die("error:".mysql_error());     }     mysql_select_db("myproject",$con); } 

i'm getting answer in question number 1 of examiner, because of this. not loop i'm getting 1 answer if theres 2-3 more answers.

$stud_id = $_post['s_id']; $quiz_id = $_post['q_id']; $score = 0;  $tbl_name2="a_quiz";  $sql2="select * $tbl_name2 q_id='$quiz_id'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){     $q_question=$rows['q_question'];     $question1 = $rows['answer'];      echo $q_question;     echo '<br>';     echo $question1;     echo '<br>';     if(isset($_post[$q_question])){         $s_answer = $_post[$q_question];         echo $s_answer;         if ($question1 == $s_answer){             $score = $score + 1;         }     } } echo $score; 

so, example, got question 1 , question 2 , selected correct answer in number 1 i'm getting , $score++ when answer in question 2 correct doesn't $score++.

my problem here is: how can value of radio button outputted them in while loop? need while loop answer too? how?

first, using each question name attribute radio buttons, far perfect. might consider add column table 'a_quiz' unique namespace each question no space in (for instance question-one, question-two...) , call column 'namespace'.

after done, questions loop should this:

<?php $score = 0; $tbl_name2="a_quiz"; // switch table "forum_answer" $sql2="select * $tbl_name2 q_id='$id'"; $result2=mysql_query($sql2);  while($rows=mysql_fetch_array($result2)){      $q_question = $rows['q_question'];     $namespace = $rows['namespace'];  ?>  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc"> <tr> <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff"> <tr style='overflow:hidden; word-wrap:break-word;'>     <input type="text" name="q_id" value="<?php echo $rows['q_id'];?>"> <td bgcolor="lightgreen"><strong>question:</strong></td> <td bgcolor="lightgreen">:</td> <td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question']; ?></td> </tr>  <tr> <td bgcolor="lightgreen"><strong>a</strong></td> <td bgcolor="lightgreen">:</td> <td bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="a"><?php echo $rows['a'] ?></td> </tr> <tr> <td width="18%" bgcolor="lightgreen"><strong>b</strong></td> <td width="5%" bgcolor="lightgreen">:</td> <td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="b"><?php echo $rows['b'] ?></td> </tr> <tr> <td width="18%" bgcolor="lightgreen"><strong>c</strong></td> <td width="5%" bgcolor="lightgreen">:</td> <td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="c"><?php echo $rows['c'] ?></td> </tr> <tr> <td bgcolor="lightgreen"><strong>d</strong></td> <td bgcolor="lightgreen">:</td> <td bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="d"><?php echo $rows['d'] ?></td> </tr>   </table></td> </tr> <hr> </table><br>  <?php } ?> 

then, results part, use code:

$stud_id = (int)$_post['s_id']; $quiz_id = (int)$_post['q_id']; $score = 0;  $tbl_name2 = "a_quiz";   $sql2 = "select * $tbl_name2 q_id='$quiz_id'"; $result2 = mysql_query($sql2);  $result = '';  while($rows=mysql_fetch_array($result2)){      $q_question = $rows['q_question'];     $question1 = $rows['answer'];     $namespace = $rows['namespace'];      $result .= $q_question;     $result .= <br>;     $result .= $question1;     $result .= <br>;      if(isset($_post[$namespace])){          $s_answer = $_post[$namespace];          $result .= $s_answer;          if ($question1 == $s_answer)             $score++;      }  }  $result .= $score;  echo $result;  } ?> 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -