【问题标题】:Pagination: A big issues分页:一个大问题
【发布时间】:2017-10-23 11:22:07
【问题描述】:

我正在尝试修复分页数据库表。使用下面看到的分页脚本后 我不想调用整个数据库,我只想从用户登录帐户调用数据库,然后 不是数据库中的整个表,...我如何使用下面的分页脚本来实现这一点

没有分页,这是我使用的声明,它完美地工作

$sql = "SELECT * FROM tbl_transaction WHERE to_accno = $acc_no 
        ORDER BY id DESC LIMIT 20";
$result = dbQuery($sql);

:
=====================================
WITH PAGINATION HERE IS MY CODE BELOW
THE ISSUES IS THAT IF A LOG INTO HIS USER PANEL, IT SHOW ALL
USERS TABLE FROM DATABASE IN A USER PANEL
========================================
<?php  
//pagination.php  
require_once '../library/config.php';
require_once '../library/functions.php';

$_SESSION['user_return_url'] = $_SERVER['REQUEST_URI'];
checkUser();

 $record_per_page = 10;  
 $page = '';  
 $output = '';  
 if(isset($_POST["page"]))  
 {  
      $page = $_POST["page"];  
 }  
 else  
 {  
      $page = 1;  
 }  
 $start_from = ($page - 1)*$record_per_page;  
 $query = "SELECT * FROM tbl_transaction ORDER BY to_accno DESC LIMIT $start_from, $record_per_page";  
 $result = mysqli_query($connect, $query);  
 $output .= "  
      <table class='table table-striped table-hover table-bordered'>  
           <thead class='thead-inverse'> <tr> 
                <th>Trns Date</th>  
                <th>Description</th>  
                <th>Debit ($)</th>  
                <th>Credit($)</th>  
                <th>Remark</th>  
           </tr>  
 ";  
 while($row = mysqli_fetch_array($result))  
 {  
      $output .= '  
           <tr>  
                 <td>'.$row["tdate"].'</td>  
                 <td>'.$row["comments"].'</td> 
                 <td>'.$row["tx_type == debit ? &nbsp; . number_format($amount, 2)"].'</td>
                 <td>'.$row["tx_type == credit ? &nbsp; . number_format($amount, 2)"].'</td>
                <td>'.$row["remark"].'</td>  
           </tr>  
      ';  
 }  
 $output .= '</table><br /><div align="center">';  
 $page_query = "SELECT * FROM tbl_transaction ORDER BY to_accno DESC";  
 $page_result = mysqli_query($connect, $page_query);  
 $total_records = mysqli_num_rows($page_result);  
 $total_pages = ceil($total_records/$record_per_page);  
 for($i=1; $i<=$total_pages; $i++)  
 {  
      $output .= "<span class='pagination_link' style='cursor:pointer; padding:6px; border:1px solid #ccc;' id='".$i."'>".$i."</span>";  
 }  
 $output .= '</div><br /><br />';  
 echo $output;  
 ?>  

【问题讨论】:

  • 嗯...在您的查询中添加WHERE ... 子句...!?
  • $_POST 对此无效,请用作 $_GET 或 $_REQUEST
  • 能否将问题标题和问题正文中的大写字母更改为小写或驼峰式?这被认为是你在大喊大叫。
  • 优化一个问题通常比创建一个新问题要好。看这里stackoverflow.com/a/46908059/2915423,也许这可以解决这两个问题,因为根本原因看起来是一样的。

标签: php


【解决方案1】:
    $perpage = 5;

    if(isset($_GET["page"])){
    $page = intval($_GET["page"]);
    }
    else {
    $page = 1;
    }
    $calc = $perpage * $page;
    $start = $calc - $perpage;

    $query = "SELECT * FROM tbl_transaction ORDER BY to_accno DESC Limit $start, $perpage";  
    $result = mysqli_query($connect, $query); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多