首页 > 代码库 > 练习:租房子

练习:租房子

技术分享

 

示例图:

技术分享

图1

技术分享

图2                                            图3

技术分享

图4

 

题目做法:

建立数据库

技术分享

技术分享

技术分享

 

封装类文件:

class fengzhuang
{
    public $host="localhost";
    public $uid="root";
    public $pwd="159357";
    public $sjkname="housedb";


//返回二维数组    
    function query($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type==1)
        {
            return $reslut->fetch_all();
        }
        else
        {
            return $reslut;
        }
    }



//返回关联数组
    
    function glquery($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type==1)
        {
            $attr = array();
            while($a = $reslut->fetch_assoc())        //取一条
            {
                $attr[] = $a;
            }
            return $attr;
            
        }
        else
        {
            return $reslut;
        }
    }
    
    
    
//返回接字符串
    
    function zfcquery()
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type=1)
        {
            $attr = $reslut->fetch_all();        //二维数组不能用implode
            
            $atr = "";
            foreach($attr as $v)
            {
                $str .= implode("^",$v);
                $str .= "|";
            }
            return substr($str,0,strlen($str)-1);
        }
        else
        {
            return $reslut;
        }
    }
}

首页:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>操作</td>
        <td>代号</td>
        <td>关键字</td>
        <td>所属区域</td>
        <td>使用面积(平米)</td>
        <td>租金(每月)</td>
        <td>租赁类型</td>
        <td>房屋类型</td>
    </tr>

<?php

include("fengzhuang.class.php");
$db = new fengzhuang();

$sql = "select * from house";
$attr = $db->query($sql);


foreach($attr as $v)
{
    echo "<tr>";
    echo "<td><a href=http://www.mamicode.com/‘shanchu.php?c={$v[0]}‘ onclick=\"return confirm(‘主人真的不要我了吗?‘)\">删除</a><a href=http://www.mamicode.com/‘xiugai.php?c={$v[0]}‘>修改</a></td>
        <td>{$v[0]}</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>";
    echo "</tr>";
}


?>

</table>
<br />

<a href="http://www.mamicode.com/tianjia.php"><input type="submit" value="http://www.mamicode.com/添加" /></a>
<a href="http://www.mamicode.com/tjsousuo.php"><input type="submit" value="http://www.mamicode.com/条件搜索" /></a>

添加页面:

<form action="tianjiacl.php" method="post">

<div><input type="hidden" name="id" /></div>
<div>关键字:<input type="text" name="keyword" /></div>
<div>所属区域:<input type="text" name="area" /></div>
<div>使用面积(平米):<input type="text" name="squaremeter" /></div>
<div>租金(每月):<input type="text" name="rent" /></div>
<div>租赁类型:<input type="text" name="renttype" /></div>
<div>房屋类型:<input type="text" name="housetype" /></div>

<input type="submit" value="http://www.mamicode.com/添加" />
</form>

<a href="http://www.mamicode.com/zhuye.php"><input type="submit" value="http://www.mamicode.com/返回" /></a>

添加处理:

<?php
$id = $_POST["id"];
$keyword = $_POST["keyword"];
$area = $_POST["area"];
$squaremeter = $_POST["squaremeter"];
$rent = $_POST["rent"];
$renttype = $_POST["renttype"];
$housetype = $_POST["housetype"];


include("fengzhuang.class.php");
$db = new fengzhuang();

$sql = "insert into house value(‘{$id}‘,‘{$keyword}‘,‘{$area}‘,‘{$squaremeter}‘,‘{$rent}‘,‘{$renttype}‘,‘{$housetype}‘)";

$db->query($sql,0);

header("location:tianjia.php");

 

删除处理:

<?php

$id = $_GET["c"];

include("fengzhuang.class.php");

$db = new fengzhuang();

$sql = "delete from house where id=‘{$id}‘";

$db->query($sql,0);

header("location:zhuye.php");

修改页面:

<?php

$id = $_GET["c"];

include("fengzhuang.class.php");

$db = new fengzhuang();

$sql = "select * from house where id=‘{$id}‘";

$ahouse = $db->query($sql);

$a = $ahouse[0];
?>


<form action="xiugaicl.php" method="post">

<div><input type="hidden" name="id" value="http://www.mamicode.com/<?php echo $a[0]; ?>" /></div>
<div>关键字:<input type="text" name="keyword" value="http://www.mamicode.com/<?php echo $a[1]; ?>" /></div>
<div>所属区域:<input type="text" name="area" value="http://www.mamicode.com/<?php echo $a[2]; ?>" /></div>
<div>使用面积(平米):<input type="text" name="squaremeter" value="http://www.mamicode.com/<?php echo $a[3]; ?>" /></div>
<div>租金(每月):<input type="text" name="rent" value="http://www.mamicode.com/<?php echo $a[4]; ?>" /></div>
<div>租赁类型:<input type="text" name="renttype" value="http://www.mamicode.com/<?php echo $a[5]; ?>" /></div>
<div>房屋类型:<input type="text" name="housetype" value="http://www.mamicode.com/<?php echo $a[6]; ?>" /></div>

<input type="submit" value="http://www.mamicode.com/修改" />
</form>

<a href="http://www.mamicode.com/zhuye.php"><input type="submit" value="http://www.mamicode.com/返回" /></a>

修改处理:

<?php
$id = $_POST["id"];
$keyword = $_POST["keyword"];
$area = $_POST["area"];
$squaremeter = $_POST["squaremeter"];
$rent = $_POST["rent"];
$renttype = $_POST["renttype"];
$housetype = $_POST["housetype"];



include("fengzhuang.class.php");

$db = new fengzhuang();

$sql = "update house set keyword=‘{$keyword}‘,area=‘{$area}‘,squaremeter=‘{$squaremeter}‘,rent=‘{$rent}‘,
    renttype=‘{$renttype}‘,housetype=‘{$housetype}‘ where id=‘{$id}‘"; $db->query($sql,0); header("location:zhuye.php");

条件搜索页面(全选):

<?php
include("fengzhuang.class.php");
$db = new fengzhuang();
?>
<form action="zufangzi.php" method="post">
<div>区域:<input type="checkbox" onclick="checkall(this,‘qx‘)" />全选</div>
<div>
    <?php
        $sarea = "select distinct area from housedb";
        $aarea = $db->Query($sarea);
        
        foreach($aarea as $v)
        {    
            echo "<input type=‘checkbox‘ value=http://www.mamicode.com/‘{$v[0]}‘ name=‘qx[]‘ class=‘qx‘ />{$v[0]} ";
        }
    ?>
</div>
<br />
<div>租赁类型:<input type="checkbox" onclick="checkall(this,‘zllx‘)" />全选</div>
<div>
    <?php
        $szllx = "select distinct renttype from housedb";
        $azllx = $db->Query($szllx);
        
        foreach($azllx as $v)
        {
            echo "<input type=‘checkbox‘ value=http://www.mamicode.com/‘{$v[0]}‘ name=‘zllx[]‘ class=‘zllx‘ />{$v[0]} ";
        }
    ?>
</div>
<br />
<div>房屋类型:<input type="checkbox" onclick="checkall(this,‘fwlx‘)" />全选</div>
<div>
    <?php
        $sfwlx = "select distinct housetype from housedb";
        $afwlx = $db->Query($sfwlx);
        
        foreach($afwlx as $v)
        {
            echo "<input type=‘checkbox‘ value=http://www.mamicode.com/‘{$v[0]}‘ name=‘fwlx[]‘ class=‘fwlx‘ />{$v[0]} ";
        }
    ?>
</div>
<br />
<div>关键字:<input type="text" name="keyword" /></div>

<div><input type="submit" value="http://www.mamicode.com/查询" /></div>
</form>

<br />
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>关键字</td>
        <td>区域</td>
        <td>建筑面积</td>
        <td>租金</td>
        <td>租赁类型</td>
        <td>房屋类型</td>
    </tr>
    
    <?php
    
        //多条件查询
        $tj1 = " 1=1 ";
        $tj2 = " 1=1 ";
        $tj3 = " 1=1 ";
        $tj4 = " 1=1 ";
        
        //判断区域
        if(!empty($_POST["qx"])&& count($_POST["qx"])>0)
        {
            $str = implode("‘,‘",$_POST["qx"]);
            $tj1 = " area in (‘{$str}‘) ";
        }
        //判断租赁类型
        if(!empty($_POST["zllx"])&&count($_POST["zllx"])>0)
        {
            $str = implode("‘,‘",$_POST["zllx"]);
            $tj2 = " renttype in (‘{$str}‘) ";
        }
        //判断房屋类型
        if(!empty($_POST["fwlx"])&&count($_POST["fwlx"])>0)
        {
            $str = implode("‘,‘",$_POST["fwlx"]);
            $tj3 = " housetype in (‘{$str}‘) ";
        }
        //判断关键字
        if(!empty($_POST["keyword"]))
        {
            $tj4 = " keyword like ‘%{$_POST[‘keyword‘]}%‘ ";
        }
        
        
        
        
        $sall = "select * from housedb where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
        
        $aall = $db->Query($sall);
        echo $sall;
        foreach($aall 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>";
        }
    ?>







<script type="text/javascript">
function checkall(d,a)
{
    var ck = document.getElementsByClassName(a);
    
    for(var i=0;i<ck.length;i++)
    {
        if(d.checked)
        {
            ck[i].setAttribute("checked","checked");
        }
        else
        {
            ck[i].removeAttribute("checked");
        }
    }
}
</script>

 

练习:租房子