hq金水

愿你是阳光,明媚不忧伤~
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

分页查询

Posted on 2016-08-08 16:44  hq金水  阅读(152)  评论(0编辑  收藏  举报

要求:根据条件进行查询,显示查询后的分页页面

说明:

1、根据条件查询到所有记录,在调用分页limit方法实现分页功能;

2、浏览器默认get方式传值;用post方式传值,只有在点击查询按钮时可以实现效果,要先将post方式转为get方式,在判断;

代码:

get方式

<body>
<?php
include ("../config/DBDA.php");
include ("../config/page.class.php");
$dx=new DBDA();
$tj=" 1=1 ";
$name="";
//如果没有条件,就显示所有分页,如果有条件就根据条件查询,显示查询后的分页
if(!empty($_GET["name"]) && $_GET["name"]!= "")
{
    $tj=" AreaName like '%{$_GET['name']}%' ";
    $name=$_GET["name"];
}
$ztj="where".$tj;
?>
<form action="main.php" method="get">
<input type="text" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="查询"/>
</form>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<th>区域代号</th>
<th>区域名称</th>
<th>父级代号</th>
</tr>
<?php
//查询总共多少条数据
$sq="select count(*) from chinastates ".$ztj;
$result=$dx->Query($sq);
$total=$result[0][0];
//造一个分页对象
$page=new Page($total,20);
//拼一个分页条件
$sql="select * from chinastates ".$ztj.$page->limit;
$attr=$dx->Query($sql);
foreach($attr as $v)
{
    echo "<tr>
    <td align='center'>{$v[0]}</td>
    <td align='center'>{$v[1]}</td>
    <td align='center'>{$v[2]}</td>
    </tr>";
    }
?>
</table>
<?php
//显示分页信息
echo $page->fpage();
?>

post方式

<body>
<?php
include ("../config/DBDA.php");
include ("../config/page.class.php");
$dx=new DBDA();
$tj=" 1=1 ";
$name="";
//如果没有条件,就显示所有分页,如果有条件就根据条件查询,显示查询后的分页
if(!empty($_POST["name"]) && $_POST["name"]!= "")
{
    $tj=" AreaName like '%{$_POST['name']}%' ";
    $name=$_POST["name"];
}
if(!empty($_GET["name"]) && $_GET["name"]!= "")
{
    $tj=" AreaName like '%{$_GET['name']}%' ";
    $name=$_GET["name"];
}                                          //区别1
$ztj="where".$tj;
$posttj="name={$name}";
?>
<form action="main2.php" method="post">
<input type="text" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="查询"/>
</form>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<th>区域代号</th>
<th>区域名称</th>
<th>父级代号</th>
</tr>
<?php
//查询总共多少条数据
$sq="select count(*) from chinastates ".$ztj;
$result=$dx->Query($sq);
$total=$result[0][0];
//造一个分页对象
$page=new Page($total,20,$posttj);    //区别2
//在分页中默认用get方式传值,用post方式传值需要将post转化为get方式;  
//拼一个分页条件
$sql="select * from chinastates ".$ztj.$page->limit;
$attr=$dx->Query($sql);
foreach($attr as $v)
{
    echo "<tr>
    <td align='center'>{$v[0]}</td>
    <td align='center'>{$v[1]}</td>
    <td align='center'>{$v[2]}</td>
    </tr>";
    }
?>
</table>
<?php
//显示分页信息
echo $page->fpage();
?>
</body>