Php 我想将数组值存储在会话变量中。并在下一页中检索这些值

Php 我想将数组值存储在会话变量中。并在下一页中检索这些值,php,jquery,arrays,Php,Jquery,Arrays,我正在开发一个测验应用程序php。我想将测验数据和用户选择的选项合计到下一页。我想在使用时单击摘要按钮显示测验摘要。请帮助我如何执行此操作 <?php require 'dataconnection.php'; $res = mysql_query("select * from questions where category_id=$category ORDER BY RAND() LIMIT 20") or die(mysql_erro

我正在开发一个测验应用程序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);


          echo $rows;
          $i=1;
           while($result=mysql_fetch_array($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='<?php echo $result['QNo'];?>'/>                                                                      
                    <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/>                    
                    <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; ?>' /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

                    <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/>              
                    <button id='next<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
                    </div>
          <?php } $i++;  $_SESSION['Questions']['']= $result;

                    ;
                      } 
          ?>














在此页面中,将会话另存为json

$_SESSION['Questions'] = json_encode(mysql_fetch_assoc($res));
在页面中,解码json

$questions = json_decode($_SESSION['Questions']);

注意:尝试使用mysqli或PDO您使用的语法已被弃用,将从未来的php版本中删除

您需要格式化代码。每行必须至少缩进4个空格,才能被解释为代码-突出显示它并点击ctrl-K将给您一个良好的开始。但它只给出数组的最后一个值,您可以提供一个变量转储
json\u encode(mysql\u fetch\u assoc($res))它正在工作。我构建测验应用程序。我想将所选单选按钮值显示到下一页,并将这些值显示为用户过去的选择以及问题的正确答案。请帮助如何操作