5月9 租房子练习题
搜索页面:
仍然是用到加载类:在上一级的test0506文件夹中
<?php class DBDA { public $host = "localhost";//数据库地址 public $uid = "root";//数据库用户名 public $pwd = "";//数据库密码 //执行SQL语句,返回相应的方法 //参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库 public function Query($sql,$type=0,$db="mydb")//经常操作的数据库名称 { //造连接对象 $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否有误 !mysqli_connect_error() or die("连接失败"); //执行SQL语句 $result = $dbconnect->query($sql); if($type==0)//查询时肯定是返回数组的 { return $result->fetch_all(); } else//增删改时只看看执行是否成功即可 { return $result; } } }
主页面代码-------0510.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>租房子练习</title> </head> <body> <form action="0510.php" method="post"> <div>区域:<input type="checkbox" onclick="CheckAll(this,'qy')" />全选</div> <div> <?php include("../test0506/DBDA.class.php"); $db = new DBDA(); $sqlqy = "select distinct Area from House"; $attrqy = $db->Query($sqlqy); //var_dump($attrqy); foreach($attrqy as $v) { echo "<input class='qy' type='checkbox' value='{$v[0]}' name='qy[]'/>{$v[0]} "; } ?> </div><br /> <div>租赁类型:<input type="checkbox" onclick="CheckAll(this,'zl')" />全选</div> <div> <?php $sqlzl = "select distinct RentType from House"; $attrzl = $db->Query($sqlzl); foreach($attrzl as $v) { echo "<input class='zl' type='checkbox' value='{$v[0]}' name='zl[]'/>{$v[0]} "; } ?> </div><br /> <div>房屋类型:<input type="checkbox" onclick="CheckAll(this,'fw')" />全选</div> <div> <?php $sqlfw = "select distinct HouseType from House"; $attrfw = $db->Query($sqlfw); //var_dump($attrqy); foreach($attrfw as $v) { echo "<input class='fw' type='checkbox' value='{$v[0]}' name='fw[]'/>{$v[0]} "; } ?> </div><br /> <div>关键字:<input type="text" name="keyword" id="key" /> </form> <br /> <input type="submit" value="搜索" /> <br /> <br /> <br /> <table cellpadding="0" cellspacing="0" border="1" width="100%"> <tr> <td>关键字</td> <td>区域</td> <td>面积</td> <td>租金</td> <td>租赁类型</td> <td>房屋类型</td> </tr> <?php $tj = ""; $tj1 = "1=1"; $tj2 = "1=1"; $tj3 = "1=1"; $tj4 = "1=1"; if(!empty($_POST["qy"])) { $attr = $_POST["qy"]; $str = implode("','",$attr); $tj1 = " Area in ('{$str}')"; } if(!empty($_POST["zl"])) { $attr = $_POST["zl"]; $str = implode("','",$attr); $tj2 = " RentType in ('{$str}')"; } if(!empty($_POST["fw"])) { $attr = $_POST["fw"]; $str = implode("','",$attr); $tj3 = " HouseType in ('{$str}')"; } if(!empty($_POST["keyword"])) { $attr = $_POST["keyword"]; $tj3 = " keyword like '%{$attr}%'"; } $tj = " Where {$tj1} and {$tj2} and {$tj3} and {$tj4}"; $sql = "select * from House".$tj; $attrall = $db->Query($sql); //var_dump($attrall); foreach($attrall as $v) { echo "<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[6]}</td> </tr>"; } ?> </table> </body> <script type="text/javascript"> function CheckAll(a,b)//this表示该 { var qx = a.checked; var ck = document.getElementsByClassName(b); for(var i =0;i<ck.length;i++) { ck[i].checked = qx; } } </script> </html>
初始页面效果
增删改的练习:
主页面-----Main.php(都是在test0509中)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <table width="100%" cellpadding="0" cellspacing="0" border="1"> <tr> <td></td> <td></td> <td>关键字</td> <td>区域</td> <td>使用面积</td> <td>租金</td> <td>租赁类型</td> <td>房屋类型</td> </tr> <?php include("../test0506/DBDA.class.php"); $db = new DBDA(); $sql = "select * from House"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<tr> <td><a href='update.php?id={$v[0]}'>编辑</a></td> <td><a href='delete.php?id={$v[0]}'>删除</a></td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[6]}</td> </tr>"; } ?> </table><br /> <div> <a href="Add.php"><input type="button" value="添加数据" /></a> <a href="search.php"><input type="button" value="搜索查询" /></a> </div> </body> </html>
页面显示效果
添加房屋信息的页面----Add.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>添加信息</h1> <form action="Addchuli.php" method="post"> <div>关键字: <input type="text" name="keyword" style="width:350px; height:30px" /></div><br /> <div>区域: <input type="text" name="area" style="width:350px; height:30px" /></div><br /> <div>使用面积: <input type="text" name="squaremeter" style="width:150px; height:30px" /></div><br /> <div>租金: <input type="text" name="rent" style="width:350px; height:30px" /></div><br /> <div>租赁类型: <input type="text" name="renttype" style="width:350px; height:30px" /></div><br /> <div>房屋类型: <input type="text" name="housetype" style="width:350px; height:30px" /></div><br /> <br /> <input type="submit" value="插入信息" /> <a href="Main.php"><input type="button" value="返回主页" /></a> </form> </body> </html>
页面显示效果
添加处理页面----Addchuli.php
<?php $keyword = $_POST["keyword"]; $area = $_POST["area"]; $squaremeter = $_POST["squaremeter"]; $rent = $_POST["rent"]; $renttype = $_POST["renttype"]; $housetype = $_POST["housetype"]; include("../test0506/DBDA.class.php"); $db = new DBDA(); $sql = "insert into house values('','{$keyword}','{$area}','{$squaremeter}','{$rent}','{$renttype}','{$housetype}')"; $result = $db->Query($sql,1); if($result) { header("location:Main.php"); } else { echo "添加失败"; }
删除页面-----delete.php
<?php //没有用加载类 $id = $_GET["id"]; $db = new MySQLi("localhost","root","","mydb"); $sql = "delete from house where id = '{$id}'"; $result = $db->query($sql); if($result) { header("location:Main.php"); } else { echo "删除失败"; }
修改信息----update.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>修改页面</h1> <?php include("../test0506/DBDA.class.php"); $db = new DBDA(); $id = $_GET["id"]; $sql = "select * from house where id = '{$id}'"; $attr = $db->Query($sql); //var_dump($attr); ?> <form action="updatechuli.php" method="post"> <div>关键字: <input type="text" name="keyword" style="width:350px; height:30px" value="<?php echo $attr[0][1] ?>" /></div><br /> <div>区域: <input type="text" name="area" style="width:350px; height:30px" value="<?php echo $attr[0][2] ?>" /></div><br /> <div>使用面积: <input type="text" name="squaremeter" style="width:150px; height:30px" value="<?php echo $attr[0][3] ?>" /></div><br /> <div>租金: <input type="text" name="rent" style="width:350px; height:30px" value="<?php echo $attr[0][4] ?>" /></div><br /> <div>租赁类型: <input type="text" name="renttype" style="width:350px; height:30px" value="<?php echo $attr[0][5] ?>" /></div><br /> <div>房屋类型: <input type="text" name="housetype" style="width:350px; height:30px" value="<?php echo $attr[0][6] ?>" /></div><br /> <br /> <input type="submit" value="修改" /> <a href="Main.php"><input type="button" value="查看" /></a> </form> </body> </html>
页面显示效果
修改处理----updatechuli.php
<?php //使用加载类 include("../test0506/DBDA.class.php"); $db = new DBDA(); $keyword = $_POST["keyword"]; $area = $_POST["area"]; $squaremeter = $_POST["squaremeter"]; $rent = $_POST["rent"]; $renttype = $_POST["renttype"]; $housetype = $_POST["housetype"]; $sql = "update house set keyword ='{$keyword}',area ='{$area}',squaremeter ='{$squaremeter}',rent ='{$rent}', renttype ='{$renttype}',housetype ='{$housetype}' where id = '{$id}' "; $attr = $db->Query($sql,1); if($attr) { header("location:Main.php"); } else { echo "修改失败"; }
以上是对于练习租房子的全部内容
以下是对于搜索页面全选,及多选的以及字符串拼接的整理
关于checkbox:多选时name是以数组的形式提交
<input type ="checkbox" name ="xx[]"/>这个只是一个类型,要实现全选功能使用JavaScript的方法
<form action="checkbox.php" method="post"> <div>全选:<input type="checkbox" id="qy" onclick="CheckAll()" /></div> <?php include("../test0506/DBDA.class.php"); $db = new DBDA(); $sql = "select distinct Area from House"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<input class='qy' type='checkbox' value='{$v[0]}' name='qy[]'/>{$v[0]}"; } ?> </form> </body> <script type="text/javascript"> function CheckAll() { var qx = document.getElementById("qy").checked; var ck = document.getElementsByClassName("qy"); for(var i=0;i<ck.length;i++) { ck[i].checked = qx; } } </script> </html>
function CheckAll()可以写参数的,像题中的this表示该,表示当前的,特别是像很多都可以用同一个方法实现时
紧接着上面的内容:$_POST["qy"]是以数组的形式输出的,写进SQL语句中要使用字符串才可以,因此有字符串的拼接implode,expolde是拆分字符串
<?php //var_dump($_POST["qy"]);输出取得的所有名字,以数组的形式 $attr1 = $_POST["qy"]; $str = implode("','",$attr1); $tj = " where Area in ('{$str}')"; $sql1 = "select * from House where Area in ('{$str}')"; $sql1 = "select * from House".$tj; echo $sql1; ?>