使用Where时,第一页后分页不起作用

时间:2015-07-26 07:28:43

标签: php pagination

我正在尝试列出数据库中的数据,我想列出特定的类别,所以我使用的是WHERE语句。我在第一页得到了正确的结果,但是当我点击第二页时它没有显示任何内容

<?php 

 include('dbconfig.inc.php');
        $id=$_GET['id'];

        $query = "SELECT * FROM distdb WHERE BANK_ID = '$id'";       
        $records_per_page=10;
        $newquery = $paginate->paging($query,$records_per_page);
        $paginate->dataview($newquery);
        $paginate->paginglink($query,$records_per_page);        
        ?>

和我的Pagination.php

<?php

class paginate
{
    private $db;

    function __construct($dbh)
    {
        $this->db = $dbh;
    }

    public function dataview($query)
    {
        $stmt = $this->db->prepare($query);
        $stmt->execute();

        if($stmt->rowCount()>0)
        {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
            {

    $bank = $row['BANK'];
    $ifsc = $row['IFSC'];
    $branch= $row['BRANCH'];
    $micr = $row['MICR_CODE'];
    $address = $row['ADDRESS'];
    $contact = $row['CONTACT'];
    $city = $row['CITY'];
    $district = $row['DISTRICT'];
    $state = $row['STATE'];

    $bankcode=$row['IFSC'];
    $brcode = substr($bankcode, -6);
                ?>
                <tr>
    <td width="21%"><?=$bank?></td> 
       <td width="20%"><?=$branch?></td>
       <td width="20%"><b>IFSC:</b><?=$ifsc?> <br /><b>MICR:</b><?=$micr?><br /><b>Branch Code:</b><?=$brcode?></td>
       <td width="24%"><?=$address?><br /> <b>City :</b><?=$city?> <br /> <b>District :</b><?=$district?> <br /> <b>State:</b> <?=$state?></td> 
       <td width="15%"><?=$contact?></td>
  </tr>


                <?php
            }
        }
        else
        {
            ?>
            <tr>
            <td>Nothing here...</td>
            </tr>
            <?php
        }

    }

    public function paging($query,$records_per_page)
    {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
    }

    public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><tr><td colspan="3"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<a href='".$self."?page_no=1'>First</a>&nbsp;&nbsp;";
                echo "<a href='".$self."?page_no=".$previous."'>Previous</a>&nbsp;&nbsp;";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
                }
                else
                {
                    echo "<a href='".$self."?page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<a href='".$self."?page_no=".$next."'>Next</a>&nbsp;&nbsp;";
                echo "<a href='".$self."?page_no=".$total_no_of_pages."'>Last</a>&nbsp;&nbsp;";
            }
            ?></td></tr><?php
        }
    }
}

?>

当我只使用下面的语句但我想用Where

过滤数据时它的工作正常
$query = "SELECT * FROM distdb"; 

3 个答案:

答案 0 :(得分:0)

在您的分页中,您只传递了您的页码,但没有银行ID。因此,在您的第二页和其他页面中,$ id = $ _ GET ['id']将为NULL。您的查询将如下所示:

$query = "SELECT * FROM distdb WHERE BANK_ID = ''";

你将获得空白结果或空白BANK_ID的结果。

    public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];
        $id=$_GET['id'];
        $self .= '?id=' . $id;
        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><tr><td colspan="3"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<a href='".$self."&page_no=1'>First</a>&nbsp;&nbsp;";
                echo "<a href='".$self."&page_no=".$previous."'>Previous</a>&nbsp;&nbsp;";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<strong><a href='".$self."&page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
                }
                else
                {
                    echo "<a href='".$self."&page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<a href='".$self."&page_no=".$next."'>Next</a>&nbsp;&nbsp;";
                echo "<a href='".$self."&page_no=".$total_no_of_pages."'>Last</a>&nbsp;&nbsp;";
            }
            ?></td></tr><?php
        }
    }

答案 1 :(得分:0)

您可以使用OFFSET吗?对于简单的$offset = ($_GET['page'] - 1) * $per_page;,请将其添加到查询的末尾:YOUR-QUERY OFFSET $offset。如果$offset为空(或零),则无需添加。

答案 2 :(得分:-2)

用这个替换你的查询

$query="SELECT * FROM distdb WHERE BANK_ID = $id"

因为$id是一个变量而不是一个字符串