练习:租房子

示例图:

图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=‘shanchu.php?c={$v[0]}‘ onclick=\"return confirm(‘主人真的不要我了吗?‘)\">删除</a><a href=‘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="tianjia.php"><input type="submit" value="添加" /></a>
<a href="tjsousuo.php"><input type="submit" value="条件搜索" /></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="添加" />
</form>

<a href="zhuye.php"><input type="submit" value="返回" /></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="<?php echo $a[0]; ?>" /></div>
<div>关键字:<input type="text" name="keyword" value="<?php echo $a[1]; ?>" /></div>
<div>所属区域:<input type="text" name="area" value="<?php echo $a[2]; ?>" /></div>
<div>使用面积(平米):<input type="text" name="squaremeter" value="<?php echo $a[3]; ?>" /></div>
<div>租金(每月):<input type="text" name="rent" value="<?php echo $a[4]; ?>" /></div>
<div>租赁类型:<input type="text" name="renttype" value="<?php echo $a[5]; ?>" /></div>
<div>房屋类型:<input type="text" name="housetype" value="<?php echo $a[6]; ?>" /></div>

<input type="submit" value="修改" />
</form>

<a href="zhuye.php"><input type="submit" value="返回" /></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=‘{$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=‘{$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=‘{$v[0]}‘ name=‘fwlx[]‘ class=‘fwlx‘ />{$v[0]} ";
        }
    ?>
</div>
<br />
<div>关键字:<input type="text" name="keyword" /></div>

<div><input type="submit" value="查询" /></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>
时间: 2024-08-08 22:08:51

练习:租房子的相关文章

PHP-----练习-------租房子-----增删改查,多条件查询

练习-------租房子-----增删改查,多条件 一 .题目要求: 二 .做法: [1]建立数据库 [2]封装类文件------DBDA.class.php 1 <?php 2 class DBDA 3 { 4 public $fuwuqi="localhost"; //服务器地址 5 public $yonghuming="root";//用户名 6 public $mima="";//密码 7 8 public $dbconnect;

练习题投票和租房子

一:投票,连个页面都引用了DBDA封装类 <!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

增删改查租房子练习题

租房子练习题:重要颜色 1.模仿get传值到处理php页面:<a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除吗?')\">删除</a> 2.删除页面显示确定删除提示框:<a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除吗?')\">删除</a> 主页面: <body

php封装+租房子练习题

第一个页面DBDA.class.php <?php class DBDA { public $host = "localhost"; public $uid = "root"; public $pwd = "root"; public $dbname = "dbname"; public function Query($sql,$type=1)//两个参数,第二个参数代表默认值 { $db = new MySQLi($t

11月6日上午PHP练习《租房子》解析

一.题目要求 二.题目做法 1.建立数据库 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 public $yonghuming="root";//用户名 public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句 //$type代表SQL语句的类型,1代表查询,2代表增删

php之租房子练习

一.题目要求 二.题目做法 1.建立数据库 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 public $yonghuming="root";//用户名 public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句 //$type代表SQL语句的类型,1代表查询,2代表增删

php租房子批量搜索例题

租房子例题是用来练习多条件查询以及批量选择的典型例题,题面是这样的: 首先要分别建立区域.租赁类型.房屋类型的复选界面,遍历出所有数据,这里用无边框表格创建: <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td>区域:<input type="checkbox" name="qx" onclic

php练习 租房子

一.题目要求 做法 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 public $yonghuming="root";//用户名 public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句 //$type代表SQL语句的类型,1代表查询,2代表增删改 //$shujuku

PHP 练习3:租房子

一.题目要求 二.题目做法 1.建立数据库 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 public $yonghuming="root";//用户名 public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句 //$type代表SQL语句的类型,1代表查询,2代表增删

复选框式查询 例题租房子

<!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-