douzhong5902 2013-09-15 09:48
浏览 88
已采纳

分页给出了额外的空白页面

Hello guys I got a problem my pagination got extra blank pages

I have 64 items and each page it display 8 items so my expected pages is from page 1 to page 8

But when I use it my 9 to page 12 is blank.

Same as my other category which only have 1 item and it go 12 pages so the rest 11 pages is blank please kindly help me thanks in advance!

Here's my code

<?php


    $tbl_name="tbl_product";        //your table name
     // How many adjacent pages should be shown on each side?
  $adjacents = 3;

  /* 
     First get total number of rows in data table. 
     If you have a WHERE clause in your query, make sure you mirror it here.
  */
  $query = "SELECT COUNT(*) FROM $tbl_name";
  $total_items = mysql_fetch_array(mysql_query($query));

  /* Setup vars for query. */
  $targetpage = "category.php";   //your file name  (the name of this file)
  $limit = 8;                 //how many items to show per page
  if(isset($_GET['page'])) {
    $page = $_GET['page'];
    $start = ($page - 1) * $limit;      //first item to display on this page
  } else {
    $page = 0;
    $start = 0;               //if no page var is given, set start to 0
  }

  /* Get data. */
  $sql = "SELECT * FROM $tbl_name where categoryID = '$category_id' LIMIT $start, $limit";
  $result = mysql_query($sql);

  /* Setup page vars for display. */
  if ($page == 0) $page = 1;          //if no page var is given, default to 1.
  $prev = $page - 1;              //previous page is page - 1
  $next = $page + 1;              //next page is page + 1
  $lastpage = ceil($total_items[0]/$limit);    //lastpage is = total pages / items per page, rounded up.
  $lpm1 = $lastpage - 1;            //last page minus 1

  /* 
    Now we apply our rules and draw the pagination object. 
    We're actually saving the code to a variable in case we want to draw it more than once.
  */
  $pagination = "";
  if($lastpage > 1)
  { 
    $pagination .= "<div class=\"pagination pagination-centered\"><ul>";
    //previous button
    if ($page > 1) 
      $pagination.= "<li><a href=\"$targetpage?categoryID=$category_id&page=$prev\">previous</a></li>";
    else
      $pagination.= "<li class=\"disabled\"><a href=\"#\">previous</a></li>"; 

    //pages 
    if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    { 
      for ($counter = 1; $counter <= $lastpage; $counter++)
      {
        if ($counter == $page)
          $pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
        else
          $pagination.= "<li><a href=\"$targetpage?categoryID=$category_id&page=$counter\">$counter</a></li>";         
      }
    }
    elseif($lastpage > 5 + ($adjacents * 2))  //enough pages to hide some
    {
      //close to beginning; only hide later pages
      if($page < 1 + ($adjacents * 2))    
      {
        for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
        {
          if ($counter == $page)
            $pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
          else
            $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$counter\">$counter</a>";         
        }
        $pagination.= "<a href=\"#\">...</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$lpm1\">$lpm1</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$lastpage\">$lastpage</a>";   
      }
      //in middle; hide some front and some back
      elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
      {
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=1\">1</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=2\">2</a>";
        $pagination.= "<a href=\"#\">...</a>";
        for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
        {
          if ($counter == $page)
            $pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
          else
            $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$counter\">$counter</a>";         
        }
        $pagination.= "<a href=\"#\">...</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$lpm1\">$lpm1</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$lastpage\">$lastpage</a>";   
      }
      //close to end; only hide early pages
      else
      {
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=1\">1</a>";
        $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=2\">2</a>";
        $pagination.= "<a href=\"#\">...</a>";
        for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
        {
          if ($counter == $page)
            $pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
          else
            $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$counter\">$counter</a>";         
        }
      }
    }

    //next button
    if ($page < $counter - 1) 
      $pagination.= "<a href=\"$targetpage?categoryID=$category_id&page=$next\">next</a>";
    else
      $pagination.= "<li class=\"disabled\"><a href=\"#\">next</a></li>";
    $pagination.= "</ul></div>
";   
  }
?>
  • 写回答

1条回答 默认 最新

  • dpsx99068 2013-09-15 15:42
    关注

    The query you're using for counting the number of records differs from the one you use to fetch the data.

    Change this block of code:

    /*
      First get total number of rows in data table.
      If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) FROM $tbl_name WHERE categoryID = '$category_id'";
    $total_items = mysql_fetch_array(mysql_query($query));
    

    It does say in the code: "if you have a WHERE clause in your query, make sure you mirror it here."

    Comments are important sometimes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址