Home > Backend Development > PHP Tutorial > A simple example of implementing multi-condition query_PHP tutorial

A simple example of implementing multi-condition query_PHP tutorial

WBOY
Release: 2016-07-21 16:02:45
Original
1056 people have browsed it

在我们的网站设计过程中,经常会用到多条件查询,本文的源码是一个二手房屋查询的例子。在本例中,我们要实现能够通过地理位置,物业类型,房屋价格,房屋面积及信息发布日期等多个条件查询到客户所需的资料。以下是实现过程。
查询条件界面(略):
查询文件(search.php)
一、生成查询语句:
$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("lingyun");
$query="select * from message where tradetype='".$tradetype."'"; //交易类型,如出租,出售
$SQL=$SQL . "wuye='" . $wuye . "'";
if($housetype!="不限"){
$query.=" && housetype='".$housetype."'"; //房屋类型,如二室一厅,三室二厅
}
if($degree!="不限"){
$query.=" && degree='".$degree."'"; //新旧程度
}
if($wuye!="不限"){
$query.=" && wuye='".$wuye."'"; //物业类型 如住房,商铺
}
if($price2!=""){
switch($price1){
case "大于":
$query.=" && price>'".$price2."'";   //价格
break;
case "等于":
$query.=" && price='".$price2."'";
break;
case "小于":
$query.=" && price<'".$price2."'";
break;
}
}
if($area2!=""){
switch($area1){
case "大于":
$query.=" && area>'".$area2."'";  //面积
break;
case "等于":
$query.=" && area='".$area2."'";
break;
case "小于":
$query.=" && area<'".$area2."'";
break;
}
}
switch($pubdate){ //发布日期
case "本星期内":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=7";
break;
case "一个月内":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=30";
break;
case "三个月内":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=91";
break;
case "六个月内":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=183";
break;
}
if($address!=""){
$query.=" && address like '%$address%'"; //地址
}
if(!$page){
$page=1;
}
?>

二、输出查询结果:
if ($page){
$page_size=20;
$result=mysql_query($query);
#$message_count=mysql_result($result,0,"total");
$message_count=10;
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$query=$query." order by date desc limit $offset, $page_size";
$result=mysql_query($query);
if($result){
$rows=mysql_num_rows($result);
if($rows!=0){
while($myrow=mysql_fetch_array($result)){
echo "";
     echo "";
     echo "$myrow[id] $myrow[tradetype] $myrow[address] $myrow[wuye]($myrow[housetype])[$myrow[date]]";
     echo "";
     echo "详细内容";
     echo "";
         }
       }
     else echo "



没有找到满足你条件的记录
";
                                                                       align='center'>";
echo " Page ".$page."/".$page_count." ";
if ($page<=1){
echo " First Page|";
                                                                                                                                                                                                                                                                                               ; }
                                                                                              echo "|Previous page|";
                                                                                           echo "|Next page|";
                                                                                                                                                                                                           ; 
             }
             echo " ";                                                                                        " |Last page|"; > else{
echo "

There is no rental information yet!

";
echo "
";
?>


If there are any problems during use, please contact me (yk_lingyun@21cn.com), Corrections are welcome!


[The copyright of this article is jointly owned by the author and Aosuo.com. If you need to reprint, please indicate the author and source]




http://www.bkjia.com/PHPjc/316526.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316526.html

TechArticle

In our website design process, multi-condition queries are often used. The source code of this article is a second-hand house query Example. In this example, we want to achieve the ability to use geographical location, objects...





source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template