PHP分页页面零问题

时间:2014-05-07 13:49:42

标签: php pagination paging

当您转到第2页时,我的分页显示页面零(0)。不确定原因。我不想显示页面零。

我会尝试只显示必要的代码。

这是我的代码:

 <?php
 $rec_limit = 100;      
 $targetpage = "dispatch.php";  
 if (isset($_GET['page'])) 
 {
   $page = $_GET['page'];
   $offset = $rec_limit * ($page - 1);
 }
 else
 {
   $page = 1;
   $offset = 0;
 }
 *** $left_rec = countRecords() - ($page * $rec_limit); ***

 $total_records = countRecords(); // countRecords() should be self-explanatory
 $total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
 $adjacents = 2;
 $previousPage = $page - 1;
 $nextPage = $page + 1;
 $querystring = "";
 $start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
 $beginning = 1;
 $end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);

 foreach ($_GET as $key => $value)
 {
   if($key != "page") $querystring .= "$key=$value&amp;";
 }
 echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li><a href=\"$targetpage?page=$beginning&$querystring\">First</a></li>";
 if ($left_rec < $rec_limit) 
 {
   $last = $page - 1;
   echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li>";
   for($i= $start; $i <= $end; $i++)
   {
     echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
 }
 else if($page == 0)
 {
   for($i= $start; $i <= $end; $i++)
   {
     echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 else if ($page > 0)
 {
   $last = $page - 2;
   echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li> ";
   for($i= $start; $i <= $end; $i++)
   {
     echo @"<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 echo "<li><a href=\"$targetpage?page=$total_pages&$querystring\">Last</a></li>";   
 echo '</ul></div></div>';
 ?>

我非常感谢从应用程序中删除第0页的帮助。请忽略任何拼写错误或缺少引号。该代码适用于显示第0页的例外情况。

我添加了显示第0页的应用程序的图片。当我转到第2页时,它只显示第0页。之后,我不再看到第0页。

enter image description here

请让我知道我必须做什么。

感谢。

2 个答案:

答案 0 :(得分:3)

一些建议:

  1. 你真的不应该使用@来抑制错误,而是应该实例化所有变量并编写正确的代码。

  2. 不要将分页硬编码到每个页面中。相反,将其包装在可重复使用的功能中。

  3. 示例:

    // draws a menu for navigating multiple pages of content
    function paginate($page, $display, $total) {
        if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
            if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
                $query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
            } else {
                $query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
            }
        } else {
            $query = '?page=';
        }
    
        $pages = $total <= $display ? 1 : ceil($total / $display);
        $self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
        $first = '<a href="' . $self . $query. '1">first</a>';
        $prev = '<a href="' . $self . $query . ($page - 1) . '">prev</a>';
        $next = '<a href="' . $self . $query . ($page + 1) . '">next</a>';
        $last = '<a href="' . $self . $query . $pages . '">last</a>';
    
        echo '<p>';
        echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
        echo '(page ' . $page . ' of ' . $pages . ')';
        echo ($page < $pages) ? "| $next | $last" : '| next | last';
        echo '</p>';
    }
    
    // output example
    $page = isset($_GET['page']) ? $_GET['page'] : 1;
    $display = 100;
    $start = $display * $page - $display;
    $total = countRecords($start, $display);
    
    paginate($page, $display, $total);
    

答案 1 :(得分:0)

我同意@mister martin,但如果您从根本上使用此代码,请尝试更改

$start = ($page < $adjacents ? 1 : $page - $adjacents);

要:

$start = ($page < $adjacents ? $page : $page - $adjacents);

编辑:未定义的$left_rec可能有问题,请查看演示。您可以像$s一样更改$_GET['page'],因为我看到一切正常。

Demo.

<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();

// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100

$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);

$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);

echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li><a href="'.$targetpage.'?page='.$beginning.'&'.$querystring.'">First</a></li>';
 if($left_rec < $rec_limit && ($page > 1))
 {
     echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li>";
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
 } else if ($page > 1)
 {
     echo "<li><a href=\"$targetpage?page=$previousPage&$querystring\">Previous</a></li> ";
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
     echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 } else {
     for($i= $start; $i <= $end; $i++)
     {
         echo "<li  " . ((($page)==$i)? "class=\"active\"" : "") . "><a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
     }
     echo "<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 echo "<li><a href=\"$targetpage?page=$total_pages&$querystring\">Last</a></li>";  
 echo '</ul></div></div>';
 ?>