php資料庫搜尋用法

來源:互聯網
上載者:User

標籤:代碼   屬性的屬性   orm   技術   attribute   租房   method   空值   host   

實現功能

1.點擊全選,下屬選框都選中

2.選中內容可以尋找到相關內容

3.輸入關鍵字可以搜尋出與關鍵字相關的資料

代碼

<body>
    <?php
        header("Content-Type:text/html;charset=utf-8");
        //串連資料庫
        $db = new MySQLi("localhost","root","","z_house");
        !mysqli_connect_error() or die();
        $db -> query("set names utf8");
        
        //判斷是否有值傳過來
        if($_POST){

  //定義一個變數,用來串連sql語句,1=1結果為true,用來串連and,防止第一個and沒有連線物件
            $set = " where 1=1 ";

//判斷是否為有值,有值則輸出下面語句
            if(!empty($_POST["qy"])){

//將數組分割成字串
                $areaArr=implode("‘,‘",$_POST["qy"]);

//編輯sql語句,用來當查詢的條件
                $set .= " and area in (‘$areaArr‘)"; 
            }

//租賃類型
            if(!empty($_POST["zl"])){
                $zlTypeArr =implode("‘,‘",$_POST["zl"]);
                $set .=" and renttype in (‘$zlTypeArr‘)";
            }

//房屋類型
            if(!empty($_POST["fw"])){
                $houseTypeArr =implode("‘,‘",$_POST["fw"]);
                $set.= " and housetype in (‘$houseTypeArr‘)";
            }

//關鍵字搜尋
            if(!empty($_POST["gjz"])){
                $set.="and keyword like ‘%".$_POST["gjz"]."%‘";
                //$str .= " and keyword like ‘%".$_POST["kword"]."%‘ ";
            }

//定義sql語句
            $sql="select * from house " .$set;
//當為空白值時,輸出整個資料庫
        }else{
            $sql="select  * from house";
        }

        $ret=$db->query($sql);
        $arr=$ret-> fetch_all();
        
    //定義數組,輸出到使用者可選向項
        $areaArr = array("板井","三義廟","上上城","天安門東","中關村");
        $zlTypeArr = array("床位","短租","整租","合租");
        $houseTypeArr = array("1室1廳","2室1廳","3室2廳","4室2廳","平房","筒子樓");    
    ?>

//租房者登入
    <a href="denglu.php">登入</a>
    <fieldset>
        <legend>房屋出租</legend>

//在本介面處理資料
        <form action="#" method="post">
            <!--地區-->
            地區:

//定義一個自訂變數,用來選中下屬複選框,
            <input type="checkbox" qx="qy" onClick="qx(this)">全選<br>
                <?php foreach($areaArr as $v){

//根據自訂屬性的屬性值篩選出下屬複選框
                    echo "<input type=‘checkbox‘ name=‘qy[]‘ value=‘$v‘ class=‘qy‘>$v &nbsp;";
                 } ?>
                <br>
            
            <!--租賃類型-->
            租賃類型:

//定義一個自訂變數,用來選中下屬複選框,
            <input type="checkbox" qx="zl" onClick="qx(this)">全選<br>
                <?php foreach($zlTypeArr as $v){

//根據自訂屬性的屬性值篩選出下屬複選框
                    echo "<input type=‘checkbox‘ name=‘zl[]‘ value=‘$v‘ class=‘zl‘>$v &nbsp;";
                 } ?>
                <br>
            <!--房屋類型-->
            房屋類型:

//定義一個自訂變數,用來選中下屬複選框,
            <input type="checkbox" qx="fw" onClick="qx(this)">全選<br>
                <?php foreach($houseTypeArr as $v){

//根據自訂屬性的屬性值篩選出下屬複選框
                    echo "<input type=‘checkbox‘ name=‘fw[]‘ value=‘$v‘ class=‘fw‘>$v &nbsp;";
                 } ?>
                <br>
            <!--關鍵字搜尋-->
            關鍵字:<input type="text" name="gjz"><br>
            <button>搜尋</button><br>
        </form>
        <table border="1">
                <tr>
                    <td>關鍵字</td>
                    <td>地區</td>
                    <td>建築面積</td>
                    <td>租金</td>
                    <td>租賃類型</td>
                    <td>房屋類型</td>
                </tr>
                <?php

//遍曆輸出資料庫中的資訊
                foreach($arr as $a){ ?>
                    <tr>
                        <td><?php echo $a[1] ?></td>
                        <td><?php echo $a[2] ?></td>
                        <td><?php echo $a[3] ?></td>
                        <td><?php echo $a[4] ?></td>
                        <td><?php echo $a[5] ?></td>
                        <td><?php echo $a[6] ?></td>
                    </tr>
                <?php } ?>
            </table>
    </fieldset>
</body>
</html>
<script>
    function qx(obj){
        var qx=obj.getAttribute("qx");
        var int=document.getElementsByClassName(qx);
        for(var i=0;i<int.length;i++){

//將複選框變成全選
            int[i].checked = obj.checked;
        }
    }
</script>

php資料庫搜尋用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.