码迷,mamicode.com
首页 > 其他好文 > 详细

2018-2-8 租房信息的增删改和搜索

时间:2018-02-08 20:01:55      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:fun   attr   body   com   i++   doctype   ade   span   页面   

建立一个租房信息的增删改和搜索:

首先在数据库中建表zufang:

技术分享图片

代码如下:

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;

展示租房信息的页面:

技术分享图片

代码:

<!--展示租房信息的页面-->
<?php
//连接数据库
$db = new MySQLi("localhost","root","","z_zf");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");//设置字符集
$sql = "select * from zufang";//sql语句,查询zufang表的内容
$result = $db->query($sql);//执行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] ?></td>
        <td><?php echo $v[5] ?></td>
        <td><?php echo $v[6] ?></td>
        <td>
            <form action="adminChuli.php" method="post">
            <!--添加属性name,之后判断执行删除操作-->
                <input type="hidden" name="type" value="del">
                <input type="hidden" name="id" value="<?php echo $v[0]; ?>">
                <button>删除</button>
            </form>
            <!--添加属性name,之后判断执行编辑操作-->
             <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>

 

添加租房信息的页面:

技术分享图片

代码:

<!--添加租房信息的页面-->
<?php
$db = new MySQLi("localhost","root","","z_zf");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");

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

    $sql = "select id,keyword,area,squareMeter,rent,rentype,housetype from zufang 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>添加数据页面</title>
</head>

<body>
<fieldset>
    <legend>添加房屋租赁信息</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","","z_zf");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");
$type = $_POST["type"];
switch($type){
        /*如果获取到的name属性值是add,执行添加数据的操作*/
    case "add":
        $kword = $_POST["kword"];       //关键字
        $area = $_POST["area"];          //  区域
        $usearea = $_POST["usearea"];      //使用面积
        $rent = $_POST["rent"];          //租金
        $zltype = $_POST["zltype"];      //租赁类型
        $housetype = $_POST["housetype"]; //房屋类型
        //sql语句添加zufang表的信息
$sql = "insert into zufang(keyword,area,squareMeter,rent,rentype,housetype)      values(‘$kword‘,‘$area‘,‘$usearea‘,‘$rent‘,‘$zltype‘,‘$housetype‘)";
        $result = $db->query($sql);    //执行sql语句,转为结果集
        header("location:show_admin.php");    //跳转页面,展示租房信息
        break;
        /*如果获取到的name属性值是del,执行删除数据的操作*/
    case "del":
        $id = $_POST["id"];
        $sql = "delete from zufang where id = $id ";    //sql语句删除zufang表的信息
        $result = $db->query($sql);    //执行sql语句,转为结果集
        header("location:show_admin.php");    //跳转页面,展示租房信息
        break;
        /*如果获取到的name属性值是mod,执行编辑数据的操作*/
    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 zufang set ".    //sql语句编辑zufang表的信息
            "keyword=‘$kword‘,".
            "area=‘$area‘,".
            "squareMeter=$usearea,".
            "rent=$rent,".
            "rentype=‘$zltype‘,".
            "housetype=‘$housetype‘ where id = $id";
        $result = $db->query($sql);    //执行sql语句,转为结果集
        header("location:show_admin.php");    //跳转页面,展示租房信息
        break;
}

搜索租房信息页面:

技术分享图片

代码:

<!--这是搜索租房信息的页面-->
<?php
//连接数据库
$db = new MySQLi("localhost","root","","z_zf");
!mysqli_connect_error() or die("连接错误");
$db->query("set names utf8");//设置字符集

if($_POST){        //判断条件,如果接收到值
    //var_dump($_POST);
    $str = " where 1 = 1  ";    //相当于true,恒等
    if(!empty($_POST["quyu"])){        //如果接收到的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 zufang ".$str;    /*sql语句后面接str*/
}else{
    $sql = "select * from zufang";
}
//var_dump($sql);
$result=$db->query($sql);//执行sql语句,转为结果集
$arr=$result->fetch_all();
$areaArr = array("板井","三义庙","上上城","天安门东","中关村");    //定义数组存区域内容        插入第48行
$zlTypeArr = array("床位","短租","整租");    //定义数组存租房类型内容    插入第55行
$houseTypeArr = array("1室1厅","2室1厅","3室2厅","4室2厅","平房","筒子楼");//定义数组存房屋类型内容        插入第62行
?>
<!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;";    //插入$areaArr数组内容
}
?>
<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;";    //插入$zlTypeArr数组内容
}
?>
 <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;";    //插入$houseTypeArr数组内容
}
?><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);//定义变量,inputarr是所有带有qxtype属性的对象
    //遍历数组,点击全选按钮,全部选中
    for(var i = 0;i<inputArr.length;i++){    
        inputArr[i].checked = obj.checked;
    }
    
}
</script>

 

2018-2-8 租房信息的增删改和搜索

标签:fun   attr   body   com   i++   doctype   ade   span   页面   

原文地址:https://www.cnblogs.com/dns6/p/8432666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!