«
用PHP简单实现多条件查询

时间:2008-5-31    作者:Deri    分类: 分享


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

   <code><?php<br />   if ($page){<br />   $page_size=20;<br />   $result=mysql_query($query);<br />   #$message_count=mysql_result($result,0,"total");<br />   $message_count=10;<br />   $page_count=ceil($message_count/$page_size);<br />   $offset=($page-1)*$page_size;<br />   $query=$query." order by date desc limit $offset, $page_size";<br />   $result=mysql_query($query);<br />   if($result){<br />   $rows=mysql_num_rows($result);<br />   if($rows!=0){<br />   while($myrow=mysql_fetch_array($result)){<br />   echo "<tr>";<br />   echo "<td width='15' height='12'><img src='image/home2.gif' width='14' height='14'></td>";<br />   echo "<td width='540' height='12'>$myrow[id]&nbsp;$myrow[tradetype]&nbsp;$myrow[address]&nbsp;$myrow[wuye]($myrow[housetype])<font style='font-size:9pt'>[$myrow[date]]</font>";<br />   echo "</td>";<br />   echo "<td width='75' height='12'><a href='view_d.php?code=$myrow[code]' target='_blank'>详细内容</a></td>";<br />   echo "</tr>";<br />     }<br />    }<br />   else echo "<tr><td><div align='center'><img src='image/sorry.gif'><br><br>没有找到满足你条件的记录</div></td></tr>";<br />   }<br />     $prev_page=$page-1;<br />     $next_page=$page+1;<br />     echo "<div align='center'>";<br />     echo "&nbsp;第".$page."/".$page_count."页&nbsp";<br />     if ($page<=1){<br />       echo "|第一页|";<br />      }<br />     else{<br />       echo "<a href='$PATH_INFO?page=1'>|第一页|</a>";<br />       }<br />     echo " ";<br />     if ($prev_page<1){<br />       echo "|上一页|";<br />      }<br />     else{<br />       echo "<a href='$PATH_INFO?page=$prev_page'>|上一页|</a>";<br />       }<br />     echo " ";<br />     if ($next_page>$page_count){<br />       echo "|下一页|";<br />       }<br />     else{<br />       echo "<a href='$PATH_INFO?page=$next_page'>|下一页|</a>";<br />       }<br />     echo " ";<br />     if ($page>=$page_count){<br />       echo "|最后一页|";<br />        }<br />     else{<br />       echo "<a href='$PATH_INFO?page=$page_count'>|最后一页|</a>";<br />       }<br />    echo "</div>";<br />  }<br />   else{<br />     echo "<p align='center'>现在还没有房屋租赁信息!</p>";<br />    }<br />  echo "<hr width="100%" size="1">";<br /> ?><br />  </table></code></p>