首页 > 代码库 > php封装+租房子练习题

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($this->host,$this->uid,$this->pwd,$this->dbname);
        $result = $db->query($sql);
        
        if($type=="1")//定义1类型的是查询,其它类型是增删改
        {
            return $result->fetch_all();
        }else
        {
            return $result;
        }
    }
}

 

 

第二个页面main.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="main.php" method="post">
<div>
    <div>区域:<input type="checkbox" onclick="xuan(this,‘qx‘)" />全选</div>//传入两个参数
    <div>
        <?php
        include("./DBDA.class.php");//引入类文件
        $db = new DBDA();
        $sqx = "select distinct area from housedb";
        $aqx = $db->Query($sqx);
        
        foreach($aqx as $v)
        {
            echo "<input type=‘checkbox‘ name=‘qx[]‘ class=‘qx‘ value=http://www.mamicode.com/‘{$v[0]}‘ />{$v[0]}";
        }
        ?>
    </div>
</div>
<br />
<div>
    <div>租赁类型:<input type="checkbox" onclick="xuan(this,‘zl‘)" />全选</div>
    <div>
        <?php
        $szl = "select distinct renttype from housedb";
        $azl = $db->Query($szl);
        
        foreach($azl as $v)
        {
            echo "<input type=‘checkbox‘ name=‘zl[]‘ class=‘zl‘ value=http://www.mamicode.com/‘{$v[0]}‘ />{$v[0]}";
        }
        ?>
    </div>
</div>
<br />
<div>
    <div>房屋类型:<input type="checkbox" onclick="xuan(this,‘fw‘)" />全选</div>
    <div>
        <?php
        $sfw = "select distinct housetype from housedb";
        $afw = $db->Query($sfw);
        
        foreach($afw as $v)
        {
            echo "<input type=‘checkbox‘ name=‘fw[]‘ class=‘fw‘ value=http://www.mamicode.com/‘{$v[0]}‘ />{$v[0]}";
        }
        ?>
    </div>
</div>
<br />
<div>关键字:<input type="text" name="keywords" />
<input type="submit" value="http://www.mamicode.com/查询" />
</div>
<br />
</form>
<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["zl"]) && count($_POST["zl"])>0)
    {
         $str = implode("‘,‘",$_POST["zl"]); 
        $tj2 = " renttype in (‘{$str}‘) ";
    }
    if(!empty($_POST["fw"]) && count($_POST["fw"])>0)
    {
         $str = implode("‘,‘",$_POST["fw"]); 
        $tj3 = " housetype in (‘{$str}‘) ";
    }
    if(!empty($_POST["keywords"]))
    {
        $key = $_POST["keywords"];
        $tj4 = " keyword like ‘%{$key}%‘ ";
    }
    
    $sql = "select * from housedb where {$tj1} and {$tj2} and {$tj3} and {$tj4}";
    
    echo $sql;
    
    $arr = $db->Query($sql);    
    
    foreach($arr 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 xuan(qx,lei)//方法类似的尽量写一个,改变参数
{
    var ck = document.getElementsByClassName(lei);
    if(qx.checked)
    {
        for(var i=0;i<ck.length;i++)
        {
            ck[i].setAttribute("checked","checked");
        }
    }
    else
    {
        for(var i=0;i<ck.length;i++)
        {
            ck[i].removeAttribute("checked");
        }
    }
}
</script>
</html>

 

php封装+租房子练习题