首先是主页面,与上篇分页页面相同
1 <table width="100%" border="1" cellpadding="0" cellspacing="0"> //建表 2 <tr> //表头 3 <td>AreaCode</td> 4 <td>AreaName</td> 5 <td>ParentAreaCode</td> 6 <td>Root</td> 7 <td>Zone</td> 8 </tr> 9 <?php 10 include("../dbda.class.php"); //引入数据库类 11 include("../page.class.php"); //引入分页类 12 $db= new dbda(); 13 $sqlall = "select count(*) from chinastates ".$tj; //写sql语句,并拼上查询条件($tj 会在下面给出) 14 $attrall = $db->Query($sqlall); 15 $total = $attrall[0][0]; 16 $page = new Page($total,20,$fytj,true); 17 $limit = $page->limit; 18 $sql = "select * from chinastates ".$tj.$limit; ////写sql语句,并拼上查询条件($tj 会在下面给出) 19 $attr = $db->Query($sql); 20 foreach($attr as $v) 21 { 22 echo "<tr> 23 <td>{$v[0]}</td> 24 <td>{$v[1]}</td> 25 <td>{$v[2]}</td> 26 <td>{$v[3]}</td> 27 <td>{$v[4]}</td> 28 </tr>"; 29 } 30 ?> 31 </table> 32 <?php 33 echo "<div>".$page->fpage()."</div>"; //分页列表 34 ?>
然后在上面加查询条框
1、如果用post方法传值:
<?php $name = ""; $fytj = ""; if(!empty($_POST["name"])) //判断是否为空,也就是判断是否有提交值 { $name = $_POST["name"]; $fytj = " name={$name}"; } if(!empty($_GET["name"])) //判断是否为空,也就是判断是否有提交值 { $name = $_GET["name"]; $fytj = " name={$name}"; } $tj = " where AreaName like '%{$name}%' "; //查询条件 ?> <br /> <form action="text.php" method="post"> //用post方法传值 <div> 请输入: <input type="text" name="name" value="" /> <input type="submit" value="查询" /> </div> </form>
2、如果用get方法传值
<?php $name = ""; $fytj = ""; if(!empty($_POST["name"])) { $name = $_POST["name"]; $fytj = " name={$name}"; } if(!empty($_GET["name"])) { $name = $_GET["name"]; $fytj = " name={$name}"; } $tj = " where AreaName like '%{$name}%' "; ?> <br /> <form action="text.php" method="post"> <div> 请输入: <input type="text" name="name" value="" /> <input type="submit" value="查询" /> </div> </form>
这里需要注意的是:
1、如果用post方法传值时,构造分页对象时需要些4个参数
$page = new Page($total,20,$fytj,true);
而且要判断$_POST["name"]和$_GET["name"]是否为空
2、如果用get方法传值,则比较简单,构造分页对象时只需写两个参数
$page = new Page($total,20);
而且只需判断$_GET["name"]是否为空