donglanfu5831 2014-06-21 09:52
浏览 75

分页显示问题与数字页面

hey guys my English is not very good 'but i try to explain my self clear. i'm creating pagination and all of my code is working perfect.

The problem is, that i want to display only five numbers of pages and on click on next button i hide one and showing new one. it's looks like that.

next 12345 prev

next 23456 prev

Thank's for advice guys.

Here is my code :

 <?php

  $dbh = new PDO("mysql:host=localhost;dbname=northwind", "root", "123");


  $query = $dbh->prepare("SELECT ContactName FROM Customers");
  $query->execute();
  $numRows = $query->rowCount();


  if (isset($_GET['pn'])) {
      $pn = $_GET['pn'];
  } else {
      $pn = 1;
  }

  $startPage = 1;
  $perPage = 9;


  $lastPage = ceil($numRows / $perPage);


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

  $controls = '';


   if ($pn != $lastPage) {

        $controls .= '<a id="next" href="' . $_SERVER['PHP_SELF'] . '?pn=' . ($pn + 1) . '"> next </a>';
   }


    for ($i=1; $i <= $lastPage; $i++) {

         if ($i == $pn) {
              $background = ' red;';
         } else {
              $background = ' green;';
         }

         $controls .= '<a id="page_' . $i . '" data-page="' . $i . '" class="num" style="background:' . $background . ' " href="' . $_SERVER['PHP_SELF'] . '?pn=' . $i . '"> ' . $i . '</a>';
    }

     if ($pn != $startPage) {

           $controls .= '<a href="' . $_SERVER['PHP_SELF'] . '?pn=' . ($pn - 1) . '"> prev </a>';
     }

      $controls .= "PAGE " . $pn . " of " . $lastPage ;


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

      $query2 = $dbh->prepare("SELECT ContactName FROM Customers " . $limit . "");
      $query2->execute();

      $outputList = '';

      while($row = $query2->fetch(PDO::FETCH_OBJ)){ 

            $outputList .= '<h1>' . $row->ContactName . '</h1><hr />';

      } 
  • 写回答

1条回答 默认 最新

  • doukeng1922 2014-06-21 10:30
    关注

    I would suggest you to use more easy approach.

    Lets consider a page contains 25 results. That means: 1st Page range is: 1-25 2nd Page range is: 26-50 and so on..

    Now when a user request page 2, we should display him the results of 25-49. this means the results of (page-1)*25 till page*25-1 = results in 25-49.

    Now all we need is how to tell the SQL to take those into consideration.

    SELECT ContactName FROM Customers LIMIT 25 OFFSET 25;
    

    Will produce use the desired result.

    Now all you need is just pass the requested page with pn as you already did. And print the next 1-3(whatever) next\prev pages.

    To know how many pages there are, just do another count sql query that will give you the result and divide it by 25.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?