租房子练习

代码:

create table zufang(
id  int  auto_increment  primary key,
Keyword int comment "关键字",
Area Varchar(50) comment"所属区域",
SquareMeter int comment"使用面积(平方米)",
Rent float comment"租金(每月)",
RenType Varchar(50) comment"租赁类型",
HouseType varchar(50) comment"房屋类型"
)charset utf8;

2、将数据库中的内容展示出来

代码:

<?php
$db = new MySQLi("localhost","root","","housedb");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");
$sql = "select * from house";
$result = $db->query($sql);
$arr = $result->fetch_all();

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>展示</title>
</head>

<body>
<table border="1" width="100%">
    <tr>
        <th>关键字</th>
        <th>区域</th>
        <th>使用面积</th>
        <th>租金</th>
        <th>租赁类型</th>
        <th>房屋类型</th>
        <th>操作</th>
    </tr>
    <?php    foreach($arr as $v){    ?>
    <tr>
        <td><?php echo $v[1] ?></td>
        <td><?php echo $v[2] ?></td>
        <td><?php echo $v[3] ?>平方米</td>
        <td><?php echo $v[4] ?>.00</td>
        <td><?php echo $v[5] ?></td>
        <td><?php echo $v[6] ?></td>
        <td>
            <form action="adminChuli.php" method="post">
                <input type="hidden" name="type" value="del"><!--传值到admin处理页面进行判断-->
                <input type="hidden" name="id" value="<?php echo $v[0]; ?>"><!--将id传到处理页面-->
                <button>删除</button>
            </form>
             <form action="add.php" method="post">
                 <input type="hidden" name="type" value="mod"><!--同上-->
                <input type="hidden" name="id" value="<?php echo $v[0]; ?>">
                <button>编辑</button>
            </form>
        
        </td>
    </tr>
    <?php  }?>
</table>
<a href="add.php">
    <button >添加数据</button>
</a>


</body>
</html>

3、添加/修改/删除表中的内容:

<?php
$db = new MySQLi("localhost","root","","housedb");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");

$arr = array();
if($_POST){
    $id = $_POST["id"];
    $type = $_POST["type"];

    $sql = "select * from house where id = $id ";
    $result = $db->query($sql);
    $arr = $result->fetch_row();
}

$areaArr = array("板井","三义庙","上上城","天安门东","中关村");
$zlTypeArr = array("床位","短租","整租");
$houseTypeArr = array("1室1厅","2室1厅","3室2厅","4室2厅","平房","筒子楼");

?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $type!="" ? "修改":"添加" ?>数据页面</title>
</head>

<body>
<fieldset>
    <legend><?php echo $type!="" ? "修改":"添加" ?>房屋租赁信息</legend>
    <form action="adminChuli.php" method="post">
        <input type="hidden" name="type" value="<?php echo $type != "" ? $type : "add"?>">
        <input type="hidden" name="id" value="<?php echo $id != "" ? $id : ""?>">
        <table>
            <tr>
                <td>关键字</td>
                <td>
                    <input type="text" name="kword" value="<?php echo $arr[1] != "" ? $arr[1] : ""?>">
                </td>
            </tr>
            <tr>
                <td>区域</td>
                <td>
                    <select name="area">
        <?php
            foreach($areaArr as $v){//将选项定死
                if($arr[2] == $v){
                    echo "<option selected>$v</option>";
                }else{
                    echo "<option >$v</option>";
                }
            }
        ?>
                    </select>
                
                </td>
            </tr>
            <tr>
                <td>使用面积</td>
                <td>
                    <input type="text" name="usearea" value="<?php echo $arr[3] != "" ? $arr[3] : ""?>">
                </td>
            </tr>
            <tr>
                <td>租金</td>
                <td>
                    <input type="text" name="rent" value="<?php echo $arr[4] != "" ? $arr[4] : ""?>">
                </td>
            </tr>
            <tr>
                <td>租赁类型</td>
                <td>
                    <select name="zltype" >
        <?php
            foreach($zlTypeArr as $v){
                if($arr[5] == $v){
                    echo "<option selected>$v</option>";
                }else{
                    echo "<option >$v</option>";
                }
            }
        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>房屋类型</td>
                <td>
                <select name="housetype" >
        <?php
            foreach($houseTypeArr as $v){
                if($arr[6] == $v){
                    echo "<option selected>$v</option>";
                }else{
                    echo "<option >$v</option>";
                }
            }
        ?>
                    </select>
                </td>
            </tr>
        </table>
        
        <button>提交</button>
    </form>
</fieldset>
</body>
</html>

后台处理:

<?php
$db = new MySQLi("localhost","root","","housedb");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");

$type = $_POST["type"];
//进行判断 如果$type==add执行添加sql语句;如果$type==del执行删除sql语句;如果$type==mod执行编辑sql语句
switch($type){
    case "add":
        $kword = $_POST["kword"];       //关键字
        $area = $_POST["area"];          //  区域
        $usearea = $_POST["usearea"];      //使用面积
        $rent = $_POST["rent"];          //租金
        $zltype = $_POST["zltype"];      //租赁类型
        $housetype = $_POST["housetype"]; //房屋类型
        
$sql = "insert into house(keyword,area,squareMeter,rent,rentype,housetype)  values('$kword','$area','$usearea','$rent','$zltype','$housetype')";
        $result = $db->query($sql);
        header("location:show_admin.php");
        break;
    case "del":
        $id = $_POST["id"];
        $sql = "delete from house where id = $id ";//id是唯一的
        $result = $db->query($sql);
        header("location:show_admin.php");
        break;
    case "mod":
        $id = $_POST["id"];
        $kword = $_POST["kword"];       //关键字
        $area = $_POST["area"];          //  区域
        $usearea = $_POST["usearea"];      //使用面积
        $rent = $_POST["rent"];          //租金
        $zltype = $_POST["zltype"];      //租赁类型
        $housetype = $_POST["housetype"]; //房屋类型
        
        $sql = "update house set ".
            "keyword='$kword',".
            "area='$area',".
            "squareMeter=$usearea,".
            "rent=$rent,".
            "rentype='$zltype',".
            "housetype='$housetype' where id = $id";//id是唯一的

        $result = $db->query($sql);
        header("location:show_admin.php");

        break;
}

 

搜索信息:

代码:

<?php
$db = new MySQLi("localhost","root","","housedb");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");

if($_POST){
    //var_dump($_POST);
    $str = " where 1 = 1  ";
    if(!empty($_POST["quyu"])){
        $areaArr = implode("','",$_POST["quyu"]);
        $str .= " and area in('$areaArr') ";
    }
    if(!empty($_POST["zltype"])){
        $zltypeArr = implode("','",$_POST["zltype"]);
        $str .= " and rentype in('$zltypeArr') ";
    }
    if(!empty($_POST["housetype"])){
        $housetypeArr = implode("','",$_POST["housetype"]);
        $str .= " and housetype in('$housetypeArr') ";
    }
    if(!empty($_POST["kword"])){
        $str .= " and keyword like '%".$_POST["kword"]."%' ";
    }    
    $sql = "select * from house ".$str;
}else{
    $sql = "select * from house";
}
$result = $db->query($sql);
$arr = $result->fetch_all();


$areaArr = array("板井","三义庙","上上城","天安门东","中关村");
$zlTypeArr = array("床位","短租","整租");
$houseTypeArr = array("1室1厅","2室1厅","3室2厅","4室2厅","平房","筒子楼");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<form action="#" method="post">
区域:<input type="checkbox" qxtype="quyu" onClick="qx(this)" >全选 <br>
<?php 
foreach($areaArr as $v){
    echo "<input class='quyu' type='checkbox' name='quyu[]' value='$v'>$v&nbsp;";
}
?>
<br><br>
租赁类型:<input type="checkbox" qxtype="zityle" onClick="qx(this)" >全选 <br>
<?php 
foreach($zlTypeArr as $v){
    echo "<input class='zityle' type='checkbox' name='zltype[]' value='$v'>$v&nbsp;";
}
?>
 <br><br>
房屋类型:<input type="checkbox" qxtype="housetype" onClick="qx(this)">全选 <br>
<?php 
foreach($houseTypeArr as $v){
    echo "<input class='housetype' type='checkbox' name='housetype[]' value='$v'>$v&nbsp;";
}
?><br><br>

关键字 : <input type="text" name="kword">

<button>搜索</button>
</form>

<table border="1" width="100%">
    <tr>
        <th>关键字</th>
        <th>区域</th>
        <th>使用面积</th>
        <th>租金</th>
        <th>租赁类型</th>
        <th>房屋类型</th>
    </tr>
<?php    foreach($arr as $v){    ?>
    <tr>
        <td><?php echo $v[1] ?></td>
        <td><?php echo $v[2] ?></td>
        <td><?php echo $v[3] ?></td>
        <td><?php echo $v[4] ?></td>
        <td><?php echo $v[5] ?></td>
        <td><?php echo $v[6] ?></td>
    </tr>
    <?php  }?>
</table>
</body>
</html>

<script>
function qx(obj){
    var qxtype = obj.getAttribute("qxtype");
    var inputArr = document.getElementsByClassName(qxtype);
    for(var i = 0;i<inputArr.length;i++){
        inputArr[i].checked = obj.checked;
    }
    
}

</script>

 

posted on 2018-02-08 20:30  啦噜噜  阅读(123)  评论(0编辑  收藏  举报