如何在php分页中将url变量传递给下一页 [英] how to pass url variable to next pages in php pagination

查看:93
本文介绍了如何在php分页中将url变量传递给下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过类似的链接从URL获取值

i am trying to get the value from URL through link like

例如$ id = 5;

for example $id =5;

<a href="ifsc-bank.php?id=<?=$id?>">link 1</a>

我有大约50个喜欢的链接列表

and i have list of links around 50 like

link 1 
link 2
link 3
.
.
link 50

单击链接后,它将路由到ifsc-bank.php 并在ifsc-bank.php

when the link is clicked it will rout to ifsc-bank.php and get the value using $_GET in ifsc-bank.php

$id=$_GET['id'];
$query = "SELECT * FROM distdb WHERE BANK_ID = '$id'";

在输出中

    1 record 
    2 record2
    3 record3

`First 1 2 3 Next` is my pagination 

当我单击1时,其罚款显示相关记录,其中id = $ id 但在单击2时,它不显示任何记录.

when i click on 1 its fine its showing related records where id = $id but on click 2 its not showing any records.

我不明白为什么其余页面中的过滤器不起作用

i don't understand why the filter is not working in rest of the pages

听到是我的代码

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

分页

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

     <table align="center" width="100%" class="t_data">
     <tbody>   
      <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>
     </tbody>
     </table> 


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

           <br  /> 

           <table width="100%"   align="left"  >
           <tr>
           <td height="54">
            <?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;";
            }

            $x="";

for($i=1;$i<=$total_no_of_pages;$i++) {
  if($i==$current_page) {
     $x.= "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
  } 
  elseif ($i>5 && $i!=$total_no_of_pages) {
     $x.= " ";
  }
  else {
     $x.= "<a href='".$self."?page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
  }
}
echo $x;

            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> 
             </table> 
            <?php
        }
    }
}

 ?>

推荐答案

我需要将代码放在HTML启动之前

got answer i need to place the code before the Html start

<?php
if (isset($_GET["id"]))
    {   
        $_SESSION['page']=$_GET["id"];
        $page = $_SESSION['page'];
    }
else
{
    $page = $_SESSION['page'];
    $tableName = $page."r"; 
}

?>

这篇关于如何在php分页中将url变量传递给下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆