php的查询数据

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>
<!--调整样式-->
<style type="text/css">
.qylist
{
    float:left;
}
.zllist
{
    float:left;
}
.fwlist
{
    float:left;
}
</style>
</head>

<body>
<?php
//连接数据库,造连接对象
    include("DBDA.php");
    $db = new DBDA();
    //接收值
    @$qytj = $_POST["qy"];
    @$zltj = $_POST["zl"];
    @$fwtj = $_POST["fw"];
    @$key = $_POST["key"];
    
    //造查询字符串
    $str1 = " 1=1";
    $str2 = " 1=1";
    $str3 = " 1=1";
    $str4 = " 1=1";
    
    //判断第一个条件是否有值
    if(count($qytj)>0)
    {
        $ss = implode("','",$qytj);
        $str1 = " Area in ('{$ss}')";
    }
    //判断租赁类型
    if(count($zltj)>0)
    {
        $ss = implode("','",$zltj);
        $str2 = " RentType in ('{$ss}')";
    }
    //判断房屋类型
    if(count($fwtj)>0)
    {
        $ss = implode("','",$fwtj);
        $str3 = " HouseType in ('{$ss}')";
    }
    //判断关键字
    if($key!="")
    {
        $str4 = " KeyWord like '%{$key}%'";
    }
    
    $sqltj = " where".$str1." and".$str2." and".$str3." and".$str4;
    
    
?>
<!--form表单,查询结果提交的该页面-->
<form action="123.php" method="post">
<div><!--设置全选按钮-->
    <div style="margin-top:10px">区域:<input type="checkbox" id="qyqx" name="qyall" onclick="CheckAll(this,'qy')" />全选</div>
    <div>
    <!--通过循环输出数据库数据添加 子选项 按钮-->
        <?php
        $sqlqy = "select distinct(Area) from House";//注意去重
        $attrqy = $db->Query($sqlqy);
        for($i=0;$i<count($attrqy);$i++)
        {
            echo "<div class='qylist'>
            <input type='checkbox' name='qy[]' onClick="KZ(this,'qyqx')" class='qy' value='{$attrqy[$i][0]}' />
            {$attrqy[$i][0]}
            </div>";
        }
        ?>
    </div>
    <!--截断流-->
    <div style="clear:both"></div>
    <div style="margin-top:20px;">租赁类型:<input type="checkbox" id="zlqx" name="zlall" onclick="CheckAll(this,'zl')" />全选</div>
    
    <div>
        <?php
        $sqlzl = "select distinct(RentType) from House";
        $attrzl = $db->Query($sqlzl);
        for($i=0;$i<count($attrzl);$i++)
        {
            echo "<div class='zllist'>
            <input type='checkbox' name='zl[]' class='zl' onClick="KZ(this,'zlqx')" value='{$attrzl[$i][0]}' />
            {$attrzl[$i][0]}
            </div>";
        }
        ?>
    </div>
    <div style="clear:both"></div>
    <div style="margin-top:20px">房屋类型:<input type="checkbox" id="fwqx" name="fwall" onclick="CheckAll(this,'fw')" />全选</div>
    
       <div>
        <?php
        $sqlfw = "select distinct(HouseType) from House";
        $attrfw = $db->Query($sqlfw);
        for($i=0;$i<count($attrfw);$i++)
        {
            echo "<div class='fwlist'>
            <input type='checkbox' name='fw[]' class='fw' onClick="KZ(this,'fwqx')" value='{$attrfw[$i][0]}' />
            {$attrfw[$i][0]}
            </div>";
        }
        ?>
    </div>
    <div style="clear:both"></div>
    
    <div style="margin:20px 0px 20px 0px">关键字:<input type="text" name="key" /> &nbsp;<input type="submit" value="搜索" /></div>
    
</div>
</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
        
        $sqlall = "select * from House".$sqltj;//拼接字符串
        $rall = $db->Query($sqlall);
        
        for($i=0;$i<count($rall);$i++)
        {
            echo "<tr>
            <td>{$rall[$i][1]}</td>
            <td>{$rall[$i][2]}</td>
            <td>{$rall[$i][3]}</td>
            <td>{$rall[$i][4]}</td>
            <td>{$rall[$i][5]}</td>
            <td>{$rall[$i][6]}</td>
            </tr>";
            
        }
    
    ?>
    
</table>

</body>
<script type="text/javascript">
function CheckAll(ck,cname)
{
    //找到全选按钮的选中状态
    var zt = ck.checked;
    //找它控制的所有的checkbox,找出来的是数组
    var all = document.getElementsByClassName(cname);
    //控制所有的checkbox状态和全选的状态一致
    for(var i=0;i<all.length;i++)
    {
        all[i].checked = zt;
    }
    
}


    
function KZ(ck,ids)//注意id不能相同
{
    //判断一下全选自身的选中状态
    if(!ck.checked)
    {
        //只要其中一个未选中就取消全选状态
        document.getElementById(ids).checked = false;
    }
}
</script>
</html>
View Code

注意:  一. Id不能一样

     二. 当前面有float时,前面不再是div样式,或是面的东西会直接出现.如果下面不再用float需要用截断流 <div type="clear:both"></div> 清一下

     三.写多个字符串时 可以使用Heredoc结构形式的方法来解决该问题,首先使用定界符表示字符串(<<<),接着在“<<<“之后提供一个标识符GOD,然后是字符串,最后以提供的这个标识符结束字符串。

  1. 定界符(“<<<”)后标识符也必须遵循 PHP 中其它任何标签的命名规则:只能包含字母数字下划线,而且必须以下划线或非数字字符开始;
  2. 结尾处的女神(“GOD”)可是在新的一行等你哦;

检查下结尾处的女神(“GOD”)前后是否有空格或其它非“;”字符哦,而且不要漏了“;”;

     四.有关字符串

1、单引号开始要以单引号结束哦,双引号开始要双引号结束;

2、嵌套的时间单引号中拥抱双引号哦,双引号中拥抱单引号;

3、单引号拥抱单引号,双引号拥抱双引号,可是需要“”转义字符在场的;

4、注意单引号和双引号都是英文下的。

原文地址:https://www.cnblogs.com/sjxx/p/5340235.html