Jump to content

how to compare the radio button value stored in database.


areeshkkj

Recommended Posts

i am developing online test ,after succesful completion of the test i want to display the correct answe,in databse i have stored corect answer as  radio button values(ex 1,2,3,4,) i want to display the coreect answer with a right mark,how can i achive this?could anybody help?

here is my code

  <?php  $testid=$_GET['testid'];
include_once("header.php");
?>
<html>
<head>
<body>
<?php
include_once('connect.php');
$sqltest="select testname from test where testid='$testid'";
$sqltestresult=mysql_query($sqltest) or die(mysql_error());
while($result=mysql_fetch_array($sqltestresult))
{
 $testname=$result['testname'];


}




?>
<!--pass test name and time left -->
<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="50%" valign="middle">
                           <h1 style="margin-left:0;"><?php 
  if(isset($testname))
  {
  
  echo $testname ;
  }
  ?></h1>
                         </td>
                         <td width="50%" align="right" valign="middle">
 <ul class="tabs" id="tabs">
                <li id="showtime" style="font-weight:bold; color:#FF0000; bottom:5px; font-size:16px; float:right"></li>
</ul>


                         </td>
                        </tr></table>
<div class="div-tabs-container" class="div-tabs-container" style="width:79%;margin-left:3%" > 
        <table cellpadding="0" cellspacing="0" border="0" id="questiontable" >
        <tr>
<?php
include_once('connect.php');
$sql="select * from testquestions where testid='$testid'";
$result=mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result)>0)
{
  $data = array(); // create a variable to hold the information
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false)


{
$data[] = $row; // add the row in to the results (data) array
}
//shift off the first value
array_shift($data[0]);


$merge = call_user_func_array('array_merge', $data);


$size=sizeof($merge);
$newdata=array();
$num=1;
$correctanswerarray=array();
$selctedanswers=array();
/* echo "<form action='fetchquestion.php' method='post' id='formsubmit' onsubmit='return out()' name='test'>"; */


foreach ($merge as $key => $value ) 
{


$sql2="select qid, question,option1,option2,option3,option4,correct_answer from question where qid='$value'  ";


$result2=mysql_query($sql2) or die(mysql_error());




  while($row2=mysql_fetch_assoc($result2))
{
                  $questionid=$row2['qid'];
    $question=$row2['question'];
$option1=$row2['option1'];
$option2=$row2['option2'];
$option3=$row2['option3'];
$option4=$row2['option4'];
        $correctanswer=$row2['correct_answer'];
#array to store the correct answer.
$correctanswerarray[] = $questionid.$correctanswer; 
   #$newdata[]=$row2;






echo "


 <table class='bix-tbl-container' cellspacing='0' cellpadding='0' border='0' width='100%' id='questiontable'><tr>
                <td class='bix-td-qno jq-qno-2413'  rowspan='2' valign='top' align='left'>$num</td>
                <td class='bix-td-qtxt' valign='top'><p>$question</p></td>
            </tr>
            <tr>
                <td class='bix-td-miscell' valign='top'><table class='bix-tbl-options' id='tblOption_2413' border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td nowrap='nowrap' class='bix-td-option bix-td-radio' width='1%'  id='tdOptionNo_A_2413'>


<input type='radio' class='result-option cls_2413' name='option_$questionid' value='1'  id='disable'/>
</td><td class='bix-td-option' width='1%'>A.</td>
                         <td class='bix-td-option' width='48%' id='tdOptionDt_A_2413'><table border='0' cellpadding='0' cellspacing='0'>
        <tr>                                                          
        <td class='bix-inner-td-option'>$option1</td>                                                         
        <td id='tdAnswerIMG_A_2413' class='jq-img-answer'  valign='middle'style='display:none;padding-left:10px;'>               
           <img src='/_files/images/website/accept.png' alt='' />  
        </td>                                                         
        </tr>                                                         
        </table></td></tr><tr><td nowrap='nowrap' class='bix-td-option bix-td-radio' width='1%'  id='tdOptionNo_B_2413'>


<input type='radio' class='result-option cls_2413' name='option_$questionid'  value='2' id='disabble'/>
</td><td class='bix-td-option' width='1%'>B.</td>
                         <td class='bix-td-option' width='48%'id='tdOptionDt_B_2413'><table border='0' cellpadding='0' cellspacing='0'>
        <tr>                                                          
        <td class='bix-inner-td-option'>$option2</td>                                                         
        <td id='tdAnswerIMG_B_2413' class='jq-img-answer'  valign='middle' style='display:none;padding-left:10px;'>               
           <img src='/_files/images/website/wrong.gif' alt=''/>  
        </td>                                                         
        </tr>                                                         
        </table></td></tr><tr><td nowrap='nowrap' class='bix-td-option bix-td-radio' width='1%'  id='tdOptionNo_C_2413'>


<input type='radio' class='result-option cls_2413' name='option_$questionid'  value='3' id='disable'/>
</td><td class='bix-td-option' width='1%'>C.</td>
                         <td class='bix-td-option' width='48%' id='tdOptionDt_C_2413'><table border='0' cellpadding='0' cellspacing='0'>
        <tr>                                                          
        <td class='bix-inner-td-option'>$option3</td>                                                         
        <td id='tdAnswerIMG_C_2413' class='jq-img-answer'  valign='middle' style='display:none;padding-left:10px;'>               
           <img src='/_files/images/website/wrong.gif' alt='' />  
        </td>                                                         
        </tr>                                                         
        </table></td></tr><tr><td nowrap='nowrap' class='bix-td-option bix-td-radio' width='1%'  id='tdOptionNo_D_2413'>


<input type='radio' class='result-option cls_2413' name='option_$questionid' value='4' id='disable'/>
</td><td class='bix-td-option' width='1%'>D.</td>
                         <td class='bix-td-option' width='48%' id='tdOptionDt_D_2413'><table border='0' cellpadding='0' cellspacing='0'>
        <tr>                                                          
        <td class='bix-inner-td-option'>$option4</td>                                                         
        <td id='tdAnswerIMG_D_2413' class='jq-img-answer'  valign='middle' style='display:none;padding-left:10px;'>               
           <img src='/_files/images/website/wrong.gif' alt='' />  
        </td>                                                         
        </tr>                                                         
        </table></td></tr></table>
 \n 


<input type='hidden' name='count' value='$num' id='count'>
<input type='hidden' name='queId[]' value='$questionid' id='queId'> 
                  
    
    


                </td>
            </tr>
            </table>
";










$num++;


}


} 
  
 echo " 
      </tr>
        </table>  
   "; 
#array of correct answrer 
#echo "array of correct answer<br/>";
#print_r($correctanswerarray);
#echo "size of the coreect answer array";
   $size1=sizeof($correctanswerarray);
echo "<br/>";




}
else
{
echo " no questions available for selected test";
}
?>
 </div>
 <!-- add div right side -->
 <div style="height:500px;width:150px;float:right;margin-top:-500px;border:1px solid #99CCFF">
</div>


<div style="height:30px">
</div>
<!--  add div bottom -->
<div style="height:125px;border:1px solid #99CCFF">
</div>


<?php
 include('footer.html'); 
?>


</body>
</html>
Edited by mac_gyver
code tags around code please
Link to comment
Share on other sites

You are going to want to store the users selections in a session or even a file if you wanted to.

 

Then you can just look at all the session data instead of saving to a database.

 

If you need to save results to a database as well, then you can compare session results to database results as well.

 

 

 

If you are using all GET requests, I can see just looping through $_GET and saving them into a session

session_start();

if(isset($_GET['testid']) && trim($_GET['testid']) !=''){
    foreach($_GET as $key=>$value){
        $_SESSION[$key] = trim($value);
    }
}

Now when you get your results from mysql, you can check them against the session values.

 

as an example

if($_SESSION['key'] == $row['key']){
$correct = true;
}
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.