Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,I am developing quiz application using the php. i want to display the Quiz review after complete the quiz. here is my code in questions.php

PHP
<?php 
  require 'dataconnection.php';
    $res = mysql_query("select * from questions where category_id=$category ORDER BY RAND() LIMIT 20") or die(mysql_error());
  $rows = mysql_num_rows($res);
   $i=1;
  while($result=mysql_fetch_assoc($res)){      
   if($i==1){?>
  <div id='question<?php echo $i;?>' class='cont'>
 <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
<input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name=''/>                                                                      
                    <br/> 
                    
                <input type="hidden" id="offset" value='<?php echo $i+1; ?>' />
                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>

                    
                 </div>    

                   <?php  }

                     if($i<$rows){?>
                       <div id='question<?php echo $i;?>' class='cont'>
                        
                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/>                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' /> 
                    <button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button> 

                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' />                      
                                  

                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>

                    
                 </div>    

                   <?php  }elseif($i==$rows){?>
                     <div id='question<?php echo $i;?>' class='cont'>
                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo  $result['QNo'];?>'/><?php echo $result['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/>              

                    <input type="hidden" id="offset" value='<?php echo $i+1; ?>' /> 
                    <button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>                      
                                  
                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
                    </div>
          <?php } $i++;   $_SESSION['questions'][]=$result;
            }
          ?>




Result.php


PHP
<?php



for($i=0;$i<20;$i++) { ?>
<div id='questions'  class='container-fluid cont'>

   
                    <p class='questions' id="qname<?php echo $i;?>"> <?php echo $i+1?>.<?php echo $_SESSION['questions'][$i]['Question'];?></p>
                    <input type="radio" value="A" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];?>'/><?php echo $_SESSION['questions'][$i]['Option A'];?>
                   <br/>
                    <input type="radio" value="B" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option B'];?>
                    <br/>
                    <input type="radio" value="C" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option C'];?>
                    <br/>
                    <input type="radio" value="D" id='radio1_<?php echo $result['QNo'];?>' name='<?php echo $_SESSION['questions'][$i]['QNo'];;?>'/><?php echo $_SESSION['questions'][$i]['Option D'];?>
                    <br/>
                    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $_SESSION['questions'][$i]['QNo'];;?>' name='<?php echo $result['QNo'];?>'/>                                                                      
                    <br/> 
                </div>



<?php } 
Posted
Comments
[no name] 14-Dec-15 9:23am    
pls, can you give me a scenario of the execution ?
Your code is not totally visible.

1 solution

There is a good number of approaches, I want to point out two of them, more modern ones. I like them because they are more universal; in particular, they don't depend on what technology you use on the server side, and applicable even if you don't have server side at all (let's say, for some applications).

One approach is having multiple pages and sharing data between them purely on the client side, using just the browser features. This can be done using Web storage: https://en.wikipedia.org/wiki/Web_storage[^].

You can chose between window.localStorage and window.sessionStorage:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API[^],
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage[^],
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage[^].

Session storage is much easier, because you won't need to clear the data from the browser; if you fail to do so or just ignore this problem, the data will be eventually removed by the browser after the session has ended. With local storage, you will have to remove this data in your code, to avoid contamination of your browser's data, but you may need it if you want to continue your quiz between sessions.

Now, Web storage, essentially, works only with strings. Very likely, you will prefer storing data in more complex structures, possibly all by one key. Then you can use serialization (http://en.wikipedia.org/wiki/Serialization[^]). How? JSON, of course; JSON.stringify and JSON.parse:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^].

Note that you don't need to deal with JSON string itself, you only serialize and deserialize your data. In my article, you can find the simple code showing the technique, Web storage with JSON: JavaScript Calculator, 7. Dynamic Strict Mode Switching and Web Storage.

Another approach is to develop the whole quite in a single page. It's pretty easy to achieve if you break the content in smaller part and show one part at a time, hiding other parts. In this approach, you won't need to exchange any data between pages. See also: https://en.wikipedia.org/wiki/Single-page_application[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900