【问题标题】:PHP & MySQL PaginationPHP & MySQL 分页
【发布时间】:2011-02-06 16:26:03
【问题描述】:

我有一个 MySQL 查询

SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \' 
ORDER BY 'timestamp'`

我想为每页分页 10 个结果。我该怎么做?

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    使用查询的 LIMIT 子句来限制您从数据库中检索的结果数量。

    见:http://dev.mysql.com/doc/refman/5.1/en/select.html

    【讨论】:

      【解决方案2】:

      使用LIMIT

      SELECT *
      FROM redirect
      WHERE user_id = '35251' 
      ORDER BY timestamp
      LIMIT 40, 10
      

      40 是要跳过的记录数,10 是要显示的记录数。

      您的 PHP 也存在一些问题。您使用反引号(而不是单引号)将表名和列名括起来。而且你不应该使用字符串连接来构建你的查询。

      【讨论】:

      • 如果已知数据是安全的,那么使用字符串连接构建 sql 查询本质上没有任何问题。
      • @code_burgar:它不会给出错误的结果,但它会为每组新参数生成一个新的查询计划。您应该使用绑定参数。
      【解决方案3】:

      这是一个不错的起点:

      <?php
      
      // insert your mysql connection code here
      
      $perPage = 10;
      $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
      $startAt = $perPage * ($page - 1);
      
      $query = "SELECT COUNT(*) as total FROM redirect
      WHERE user_id = '".$_SESSION['user_id']."'";
      $r = mysql_fetch_assoc(mysql_query($query));
      
      $totalPages = ceil($r['total'] / $perPage);
      
      $links = "";
      for ($i = 1; $i <= $totalPages; $i++) {
        $links .= ($i != $page ) 
                  ? "<a href='index.php?page=$i'>Page $i</a> "
                  : "$page ";
      }
      
      
      $r = mysql_query($query);
      
      $query = "SELECT * FROM 'redirect'
      WHERE 'user_id'= \''.$_SESSION['user_id'].' \' 
      ORDER BY 'timestamp' LIMIT $startAt, $perPage";
      
      $r = mysql_query($query);
      
      // display results here the way you want
      
      echo $links; // show links to other pages
      

      【讨论】:

      • 但是,我怎样才能创建到其他页面的链接?
      • 为匹配 where 子句的所有结果选择 COUNT,将得到的数字除以 perPage 值,得到总页数,然后执行 for 循环以回显每个页面的链接。
      • 但是对于上一个呢?你能举个例子吗?我是新手。谢谢
      • 你去吧,你应该真的能从那里拿走它
      【解决方案4】:

      这是我的代码
      其中包含下一个和上一个按钮

      <?php  
          $limit = 3;  //set  Number of entries to show in a page.
          // Look for a GET variable page if not found default is 1.        
          if (isset($_GET["page"])) {    
          $page  = $_GET["page"];    
          }    
          else { $page=1;    
          } 
          //determine the sql LIMIT starting number for the results on the displaying page  
          $page_index = ($page-1) * $limit;      // 0
      
          $All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
          while($row=mysqli_fetch_array($All_Users))
          {
              //show  data in table or where you want..
          }
          $all_data=mysqli_query($con,"select count(*) from users");
          $user_count = mysqli_fetch_row($all_data);   // say total count 9  
          $total_records = $user_count[0];   //9
          $total_pages = ceil($total_records / $limit);    // 9/3=  3
          if($page >= 2){
              echo "<a href='blog.php?page=".($page-1)."' class='btn 
           customBtn2'>Previous</a>";
            }
          
          if($page<$total_pages) {
              echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";   
          }       
      ?>
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
      • @PouriaHemati 好的。我认为这个问题就足够了。我还在代码中添加了commnets以便更好地理解
      猜你喜欢
      • 2013-05-25
      • 2018-01-12
      • 2011-04-14
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2011-08-20
      相关资源
      最近更新 更多