简体   繁体   English

分页第二页不显示

[英]Pagination 2nd page not displaying

Pagination works fine when I don't use the WHERE statement in my SELECT statement.当我在 SELECT 语句中不使用 WHERE 语句时,分页工作正常。 For some reason as soon as I add additional requests in the SELECT statement, only the 1st pagination page works.出于某种原因,一旦我在 SELECT 语句中添加其他请求,只有第一个分页页有效。 So it seems like the variable data is lost after the first page is displayed.所以看起来第一页显示后变量数据丢失了。 Below is some of the code:-以下是部分代码:-

               <?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) {

Without knowing which Paginator are we talking about, I could only advise you to do something like不知道我们在谈论哪个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();

See, you where making two mistakes here:看,你在这里犯了两个错误:

  • getting your count value without considering the query.在不考虑查询的情况下获取count数值。 You should set the value of $query regardless of the existance of $_GET['query'] , and use it in your count query as well as your results query.无论$_GET['query']是否存在,您都应该设置$query的值,并在您的计数查询和结果查询中使用它。

  • binding parameters whose placeholders and values do not exist in the query you're executing.其占位符和值在您执行的查询中不存在的绑定参数。 Make sure your results query contains :query , :start and :length or you will be binding more parameters than the query has.确保您的结果查询包含:query:start:length否则您将绑定比查询更多的参数。

You should also have wrapped your statements in try/catch blocks so you could debug what was happening.您还应该将您的语句包装在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());
}

That way you would have known that the query was failing because of Invalid parameter number: parameter was not defined这样你就会知道查询失败了,因为Invalid parameter number: parameter was not defined

NOTE I have no clue about how your paginator will know about the current page, nor can I see where are you setting the itemsPerPage value.注意我不知道你的分页器如何知道当前页面,我也看不到你在哪里设置itemsPerPage值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM