【问题标题】:pagination in php and mysqlphp和mysql中的分页
【发布时间】:2012-08-13 11:47:15
【问题描述】:

我在 php 和 mysql 中为我的网站编写了分页代码,但是当没有记录时,它会给出错误 mysql_fetch_array() expects parameter 1 to be resource 在此代码中,当数据库表为空时,它将Page 0 of 0 Back -1 0
如果记录在数据库表中,那么它工作得很好。 我的php分页代码如下

 $sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
$nr = mysql_num_rows($sql);
if (isset($_GET['pn'])) {
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 
} else {
$pn = 1;
} 
$itemsPerPage = 10; 
$lastPage = ceil($nr / $itemsPerPage);
if ($pn < 1) { // If it is less than 1
$pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
} 
$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 id, firstname, country FROM myTable ORDER BY id ASC $limit");

if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
    $previous = $pn - 1;
    $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
} 
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
    $nextPage = $pn + 1;
    $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
} 
}
 $outputList = '';
  while($row = mysql_fetch_array($sql2)){ 

$id = $row["id"];
$firstname = $row["firstname"];
$country = $row["country"];

$outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />';

} 

 <h2>Total Items: <?php echo $nr; ?></h2>

  <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;"><?php print "$outputList"; ?></div>
  <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>

有人可以建议使用page 1 of 1 from 1 选项进行良好的分页吗?

【问题讨论】:

  • 缩进代码怎么样?!
  • 你做错了。把它放在纸上,然后重试。整个分页代码最多应为 15 行。
  • 我把 $pn = 0;如果不 GET pn 则错误已解决,但出现了下一页和第二页的选项。

标签: php paging


【解决方案1】:

试试这个代码。

“下一个”和“上一个”链接:

echo "<h5>Showing ".$start." to ".($start+$limit)." Records of ".$count." Records</h5>";
if($start<=($count-$limit))
{
    echo '<a style="float:right" href="'.$_SERVER['PHP_SELF'].'?start='.($start+$limit).'&limit='.$limit.'&column='.$column.'&order='.$order.'&status='.$status.'&b='.$b.'&type='.$type.'&search='.$search.'"><t1>Next</t1></a>';
}


$prev = $start-$limit;
if ($prev >= 0)
{
    echo '<a style="float:left" href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&limit='.$limit.'&column='.$column.'&order='.$order.'&status='.$status.'&b='.$b.'&type='.$type.'&search='.$search.'"><t2>Previous</t2></a>';
}

页码:

$i=0;
$l=1;
echo "<p align='center'>";
for($i=0;$i < $count;$i=$i+$limit)
{
    if($i <> $start)
    {
        echo "<a href='listing_test.php?start=$i&limit=$limit'><font face='Verdana' size='2'><b>&nbsp;$l&nbsp;</b></font></a> ";
    }
    else
    {
        echo "<font face='Verdana' size='4' color=#2E9AFE ><b>&nbsp;$l&nbsp;</b></font>";
    }        
$l=$l+1;
}
echo "</p>";

这对我来说非常有效!

这里$count是表中的总行数,$limit是要在每页上显示的LIMIT

【讨论】:

    【解决方案2】:

    您必须检查结果集是否为空
    此代码可能会对您有所帮助.. 用
    if ($nr==1){ //code if no records are available}else{//if data is available}

    将您的分页代码括起来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-06
      • 2013-05-25
      • 2018-01-12
      • 2011-04-14
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多