如何在php中移动下一页时避免未定义的偏移错误?

时间:2013-08-12 11:03:33

标签: php jquery ajax

我正在做php在线测验应用程序,我曾经计划每页20个问题,当我移动下一页时显示未定义的偏移错误。我需要使用post方法获取所有单选按钮选择值以使用我的答案array.am进行评估获取所有选中的单选按钮值。当单击下一个分页按钮时,单选按钮的选定值会在我提交按钮之前自动发布。我需要评估问题和答案而不会出现错误未定义的偏移。请帮助任何人。是我的代码.....

<?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].'" $outputList .= ($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].'" $outputList .= ($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].'"  $outputList .= ($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].'"  $outputList .= ($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>
<style type="text/css">
<!--
.pagNumActive {
    color: #000;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
    color: #000;
    text-decoration: none;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
-->
</style>  
</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"; ?>

      <input type="submit" name="submit" id="submit" value="submit">

      </form>
      </div>
    <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>  
</body>
</html>

2 个答案:

答案 0 :(得分:0)

为了避免未定义的偏移错误,对于可能未设置的每个数组键,首先应检查该值是否已设置,然后再使用它。

if( isset($array[$key]) )
如果$ array [$ key]未定义,

将返回false。

如果您需要内联,可以使用内联,如果是这样的话

$string = '<someHtml>' . (isset($array[$key]) ? $array[$key] : '') . '</someHtml>';

也许我在你的代码中遗漏了一些东西,但是我不明白你为什么要在while循环的顶部设置数组变量然后只是在while循环中使用它们。你需要在while循环之外的数组变量吗?

另外,我会避免在PHP变量中存储大量HTML,只是为了在页面的下方回显它。你最好只在正确的地方循环输出HTML。

... Some HTML 
<?php
while( $someLogicIsTue )
{
    ?>
    ... Some HTML
    <someHTML><?php echo $someVal; ?></someHtml>
    .... Some Html
    <?php
}
?>

答案 1 :(得分:0)

检查阵列变量的条件,以避免未定义的偏移

if(is_array($your_varible)){
echo $your_varible[$key];
}