1

我正在做一个在线测验应用程序,当我单击下一个按钮时,我需要将所有选定的单选按钮值保留到下一页,因为我正在使用分页。在下面的代码中,当我移动到下一页时,它显示未定义的偏移错误。我得到了在这一行 />'.$optA[$i].' 中检查了另一个语法错误,例如意外检查。我需要解决这些问题,请帮助任何人,在此先感谢

 <?php
    session_start();
    include('connection.php');
    mysql_select_db('quiz_apptitude');
    $sql = mysql_query("SELECT * FROM prob_on_age");
    $nr = mysql_num_rows($sql); 
    if (isset($_GET['pn'])) {
        $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
    } else { 
        $pn = 1;
    } 

    $itemsPerPage = 2; 

    $lastPage = ceil($nr / $itemsPerPage);

    if ($pn < 1) {
        $pn = 1; 
    } else if ($pn > $lastPage) { 
        $pn = $lastPage; 
    } 

    $centerPages = "";
    $sub1 = $pn - 1;
    $sub2 = $pn - 2;
    $add1 = $pn + 1;
    $add2 = $pn + 2;
    if ($pn == 1) {
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    } else if ($pn == $lastPage) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    } else if ($pn > 2 && $pn < ($lastPage - 1)) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
    } else if ($pn > 1 && $pn < $lastPage) {
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    }

    $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

    $sql2 = mysql_query("SELECT * FROM prob_on_age $limit") or die('error'); 
    //$total=mysql_num_rows($sql2) ;
    $i=0;
    $outputList = '';
    while($row = mysql_fetch_array($sql2)){ 

       $Question[]=$row['prob_Question'];
    $optA[]=$row['prob_OptionA'];
    $optB[]=$row['prob_OptionB'];
    $optC[]=$row['prob_OptionC'];
    $optD[]=$row['prob_OptionD'];
    $ans[]=$row['prob_Answer'];
    $Qid[$i]=$row['prob_Qid'];

        $ans[$i]=$row['prob_Answer'];
                         $r=$i+1;




        $outputList .= '<table><tr>
                  <td> '.$r.' . '.$Question[$i].'</td>
                </tr>
                <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
                </tr>
                  <tr>

                  <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" <?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/> 
                    '.$optA[$i].'</td></tr>
                    <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                    '.$optB[$i].'</td></tr>
                 <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
                   '.$optC[$i].'</td></tr>
                <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
                '.$optD[$i].'</td></tr>

                <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
                </tr></table>';

    $i++;
    }

    $count=count($Qid);
    $paginationDisplay = ""; 
    if ($lastPage != "1"){

        $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

        if ($pn != 1) {
            $previous = $pn - 1;

            $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
        } 

        $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

        if ($pn != $lastPage) {
            $nextPage = $pn + 1;

            $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '">Next</a> ';
        } 
    }

    ?>

<?php
session_start();


include('connection.php');
mysql_select_db('quiz_apptitude');
$sql = mysql_query("SELECT * FROM prob_on_age");
$nr = mysql_num_rows($sql); 
if (isset($_GET['pn'])) {
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else { 
    $pn = 1;
} 

$itemsPerPage = 2; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; 
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("SELECT * FROM prob_on_age $limit") or die('error'); 
//$total=mysql_num_rows($sql2) ;
$i=0;
$outputList = '';
while($row = mysql_fetch_array($sql2)){ 

   $Question[]=$row['prob_Question'];
$optA[]=$row['prob_OptionA'];
$optB[]=$row['prob_OptionB'];
$optC[]=$row['prob_OptionC'];
$optD[]=$row['prob_OptionD'];
$ans[]=$row['prob_Answer'];
$Qid[$i]=$row['prob_Qid'];

    $ans[$i]=$row['prob_Answer'];
                     $r=$i+1;




    $outputList .= '<table><tr>
              <td> '.$r.' . '.$Question[$i].'</td>
            </tr>
            <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
            </tr>
              <tr>

              <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" <?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/> 
                '.$optA[$i].'</td></tr>
                <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                '.$optB[$i].'</td></tr>
             <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
               '.$optC[$i].'</td></tr>
            <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
            '.$optD[$i].'</td></tr>

            <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
            </tr></table>';

$i++;


}

$count=count($Qid);








$paginationDisplay = ""; 
if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

    if ($pn != 1) {
        $previous = $pn - 1;

        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;

        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '">Next</a> ';
    } 
}

?>

    <html>
    <head>
    <title>online QiuZ</title>
    </head>
    <body>
       <div style="margin-left:64px; margin-right:64px;">
         <h2>Problem On Age</h2>
       </div> 
       <!--   <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php //echo $paginationDisplay; ?></div>-->
          <div style="margin-left:64px; margin-right:64px;">
          <form name="prob_age" id="prob_age" method="post" action="result-age.php" >
          <?php print "$outputList"; ?>

          <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
          <input type="submit" name="submit" id="submit" value="submit">

          </form>
          </div>

    </body>
    </html>
4

2 回答 2

0

好吧,您可以使用PHP Sessions和/或Cookies(对于您的情况几乎相同)来做,但是如果由于任何原因在测验中间出现问题......您将失去所有进度,用户必须重新开始.

如果测验很短,你可以这样做,但如果它 >= 中间甚至长...我会建议使用$_POST&Forms而不是分页链接。当然,同时保存进度。

有了这个,如果您想预先检查单选按钮、输入字段或任何东西......只需在下次打开该页面时调用正确的变量(也可能是另一天,因为这些值存储在您的服务器中而不是在用户机器中。)
祝你好运

于 2013-08-12T06:40:56.377 回答
0

当我检查代码时,我发现您的变量 $output 列表数据不正确。替换为以下内容:

您正在使用 if 条件在 php 变量的字符串中回显:

<?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : '';

您在 $output 变量中使用的上述行不正确。您需要更改此行如下:

$output .= ....//other code
$output .= ($optB[$i] == $_POST[$Qid[$i]]) ? ' checked="checked"' : '';
$output .= ......

如上所述替换变量<?php echo字符串中的每个。$output

编辑:

检查你下面的代码:

$outputList .='<table><tr>
              <td> '.$r.' . '.$Question[$i].'</td>
               </tr>
               <tr>  <td><input type="hidden" name="'.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$Qid[$i].'" /></td>
            </tr>
              <tr>

              <td><input type="radio"  name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optA[$i].'" 

<?php echo '.$optA[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>

//在这里,您在 php 标记中再次启动 php 标记。所以删除 php 和 echo 标签。并像我提到的那样使用它作为 $outputList .= ($optA[$i] == $_POST[$Qid[$i]]) ?' 已检查 =“已检查”' : ''; 并在它之前关闭上面的刺。并对其他人进行类似的操作。

/> 
                '.$optA[$i].'</td></tr>
                <tr> <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'" value="'.$optB[$i].'" <?php echo '.$optB[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?> />
                '.$optB[$i].'</td></tr>
             <tr> <td><input type="radio" name=" '.$Qid[$i].'"  id="'.$Qid[$i].'" value="'.$optC[$i].'"  <?php echo '.$optC[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
               '.$optC[$i].'</td></tr>
            <tr>  <td><input type="radio" name=" '.$Qid[$i].'" id="'.$Qid[$i].'"  value="'.$optD[$i].'"  <?php echo '.$optD[$i].' == $_POST[''.$Qid[$i].''] ? ' checked="checked"' : ''; ?>/>
            '.$optD[$i].'</td></tr>

            <tr>  <td><input type="hidden" name="'.$ans[$i].'" id="'.$Qid[$i].'"  value="'.$ans[$i].'" /></td>
            </tr></table>';

注意:如果您没有得到我所说的内容,请告诉我,我将发布修改后的代码。并且会修改更多你。

于 2013-08-12T06:35:15.403 回答