PHP语言 -- Smarty分页查询
分页查询
test.php
<?php include("init.inc.php");//引入Smarty模版配置文件 include("DBDA.php");//引入操作数据库文件 include("page.class.php");//引入分页类
$cx="";
$yuju="";
$name="";
//点击搜索时用post传值,点击分页时用get传值 如果post不为空,则是点击搜索,取出post里面的值,用变量$cx存储page方法的第三个变量(将值添加到URL中,并以get方式传值);如果post为空,get不为空,则是点击分页,取出get里的值,写sql语句
if(empty($_POST["name"]))
{
if(!empty($_GET["name"]))
{
$name = $_GET["name"];
$yuju = "where AreaName like '%{$name}%' "; //注意空格
}
}
else
{
$name = $_POST["name"];
$cx = "name={$name}";
$yuju = "where AreaName like '%{$name}%' "; //注意空格
}
$smarty->assign("name",$name);
$db = new DBDA();
$sqlall = "select count(*) from ChinaStates ".$yuju;
$total = $db->StrQuery($sqlall);//总条数
$page = new Page($total,20,$cx);
$sql = "select * from ChinaStates ".$yuju.$page->limit;
$attr = $db->Query($sql);
$fp = $page->fpage(); $smarty->assign("shuju",$attr);
$smarty->assign("fp",$fp);
$smarty->display("test.html");
test.html
<body> <div> <form action="test.php" method="post"> <div> 名称:<input type="text" name="name" value="<{$name}>" /> <input type="submit" value="查询" /> </div> </form> </div> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>父级代号</td> </tr> <{foreach $shuju as $v}> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td><{$v[2}></td> </tr> <{/foreach}> </table> <div><{$fp}></div> </body>