分页第2页不显示

时间:2017-09-29 10:20:05

标签: php mysql pagination

当我在SELECT语句中使用WHERE语句时,分页工作正常。出于某种原因,只要我在SELECT语句中添加其他请求,就只有第一个分页页面有效。因此,在显示第一页后,似乎可变数据丢失了。以下是一些代码: -

               <?php
                 include 'database.php';
                 include 'paginator.php';
                 $pdo = Database::connect();

                 $paginator = new Paginator();
                 $sql = "SELECT count(*) FROM customer_crm ";
                 $paginator->paginate($pdo->query($sql)->fetchColumn());

                 $query =  $_GET["query"];
                 if (isset($query))  {
                   ($_GET['query'])?('%'.$_GET['query'].'%'):'%';
                   $sql = "SELECT * FROM customer_crm WHERE firstname LIKE :query OR email LIKE :query OR telephone LIKE :query ";
                   } 
                else    {
                    $start = (($paginator->getCurrentPage()-1)*$paginator->itemsPerPage);
                    $length = ($paginator->itemsPerPage);   
                    //$sql = "SELECT * FROM customer_crm WHERE customer_group_id = $input OR date_followup= CURDATE() ORDER BY customer_group_id DESC limit $start, $length ";
                    $sql = "SELECT * FROM customer_crm ORDER BY date_followup DESC limit $start, $length ";
                    //$sql = "SELECT * FROM customer_crm WHERE customer_group_id = $input ORDER BY date_followup DESC limit $start, $length ";
                    }


                   $sth = $pdo->prepare($sql);
                   $sth->bindParam(':start',$start,PDO::PARAM_INT);
                   $sth->bindParam(':length',$length,PDO::PARAM_INT);
                   $sth->bindParam(':query',$query,PDO::PARAM_STR);
                   $sth->execute();



                   foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {

2 个答案:

答案 0 :(得分:0)

如果没有$sql = "SELECT count(*) FROM customer_crm ";,请使用where

您必须在查询中添加WHERE firstname LIKE :query OR email LIKE :query OR telephone LIKE :query";

部分为$sql = "SELECT count(*) FROM customer_crm WHERE firstname LIKE :query OR email LIKE :query OR telephone LIKE :query";

答案 1 :(得分:0)

在不知道我们在谈论哪个Paginator的情况下,我只能建议你做一些像

这样的事情
include 'database.php';
include 'paginator.php';
$pdo = Database::connect();
$paginator = new Paginator();

$query = (isset($_GET["query"]) && strlen($_GET["query"])>1)? '%'.$_GET["query"].'%':'%';

$countsql = "SELECT * FROM customer_crm WHERE firstname LIKE :query OR email LIKE :query OR telephone LIKE :query ";
$sthcount = $pdo->prepare($countsql);
$sthcount->bindParam(':query',$query,PDO::PARAM_STR);
$sthcount->execute();

$count=$sthcount->fetchColumn();
$paginator->paginate($count);

$start = (($paginator->getCurrentPage()-1)*$paginator->itemsPerPage);
$length = ($paginator->itemsPerPage); 

$sql = $countsql . ' ORDER BY date_followup DESC limit :start, :length ';

$sth = $pdo->prepare($sql);
$sth->bindParam(':start',$start,PDO::PARAM_INT);
$sth->bindParam(':length',$length,PDO::PARAM_INT);
$sth->bindParam(':query',$query,PDO::PARAM_STR);
$sth->execute();

看,你在哪里犯了两个错误:

  • 在不考虑查询的情况下获取count值。无论$query是否存在,都应设置$_GET['query']的值,并在计数查询和结果查询中使用它。

  • 绑定参数,其占位符和值在您执行的查询中不存在。确保您的结果查询包含:query:start:length,否则您将绑定比查询更多的参数。

您还应该在try/catch块中包装您的语句,以便调试正在发生的事情。

try {
    $sth = $pdo->prepare($sql);
    $sth->bindParam(':start',$start,PDO::PARAM_INT);
    $sth->bindParam(':length',$length,PDO::PARAM_INT);
    $sth->bindParam(':query',$query,PDO::PARAM_STR);
    $sth->execute();
} catch(\PDOException $e) {
    die('Error in query: '. $e->getMessage());
}

那样你就会知道查询因为失败而失败了 Invalid parameter number: parameter was not defined

注意我不清楚您的分页器如何了解当前页面,也不知道您在哪里设置itemsPerPage值。