PHP OOP 分页

标签 php mysql oop pagination

我遇到了分页问题。我已经尽我所能地进行了调试,似乎 SELECT 查询和执行存在问题。如果我取出分页部分,查询将执行并在一个长表中显示所有条目。我尝试执行一个数组、bindValue 和 bindParam 但没有任何效果,任何人都可以看到我遗漏了什么吗?

function showEmployees() {
  $count = $this->dbh->query("SELECT * FROM employee_info");
  $count->rowCount();
  $count = $count->rowCount();

  // if ($count > 0) {
  //    echo "The total amount of employees is " . $count;
  // } else {
  //    echo "There are no records in this table.";
  // }

  $page_rows = 10;
  $last_page = ceil($count / $page_rows);

  echo $last_page;

  if ($last_page < 1) {
    $last_page = 1;
  } 

  $page_num = 10;

  if (isset($_GET['pn'])) {
    $page_num = preg_replace('#[^0-9]#', '', $_GET['pn']);
  } 

  if ($page_num < 1) {  
    $page_num = 1;
  } elseif ($page_num > $last_page) {
    $page_num = $last_page;
  } 

  $limit = 'LIMIT ' .($page_num -1) * $page_rows .', '. $page_rows;     

  $query = $this->dbh->prepare("SELECT * FROM employee_info ORDER BY employee_id DESC :page_limit");
  $query->bindParam(':page_limit', $limit );
  $query->execute();


  $t = "<table name='displayEmployees' border='1' >";
  $t .="<tr>";
  $t .= "<th>Employee ID</th>";
  $t .= "<th>First Name</th>";
  $t .= "<th>Last Name</th>";               
  $t .= "</tr>";

  while($u = $query->fetch(PDO::FETCH_ASSOC)) {
    $t .="<tr>";
    $t .= "<td>{$u['employee_id']}</td>";
    $t .= "<td>{$u['first_name']}</td>";
    $t .= "<td>{$u['last_name']}</td>"; 
    $t .="</tr>";
  }

  $t .="</table>";

  return $t;
}       

最佳答案

它认为这是您处理限制的方式,尽管这应该会引发错误。

尝试:

$beginLimit = ($page_num-1)*$page_rows;
$endLimit = $page_rows;
$query = $this->dbh->prepare("SELECT * FROM employee_info ORDER BY employee_id DESC LIMIT :begin,:end");
$query->bindValue(':begin',(int)$beginLimit,PDO::PARAM_INT);
$query->bindValue(':end',(int)$endLimit,PDO::PARAM_INT);
$query->execute();

关于PHP OOP 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260525/

相关文章:

javascript - 原型(prototype)困惑 : parent prototype property affected by child prototype

Java - 初始化一个HashMap的HashMap

php - 如何在 php 中解析长值?

mysql - 现有表的 SQL 触发器

php - 选择用于网页的随机记录

MySQL只选择新记录

java - MySQL 和 Eclipse 的连接问题

php - 我不明白为什么DeleteTask.php不起作用,但RetrieveTask.php却起作用

php - 如果在获取循环内发出另一个查询,则 PDO dblib over freetds 会重置 sql server 2000 上的查询获取

php - mysql PHP 查询数组