文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学的各位小编整理了PHP-PHP实现多条件查询实例代码的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
查询文件(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;
}
?>
二、输出查询结果: 复制代码 代码如下:
<?php
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 "<tr>";
echo "<td width=''15'' height=''12''><img src=''/uploadfile/201306/21/B7124939173.gif'' width=''14'' height=''14''></td>";
echo "<td width=''540'' height=''12''>$myrow[id] $myrow[tradetype] $myrow[address] $myrow[wuye]($myrow[housetype])<font style=''font-size:9pt''>[$myrow[date]]</font>";
echo "</td>";
echo "<td width=''75'' height=''12''><a href=''view_d.php?code=$myrow[code]'' target=''_blank''>详细内容</a></td>";
echo "</tr>";
}
}
else echo "<tr><td><div align=''center''><img src=''/uploadfile/201306/21/C2124940223.gif''><br><br>没有找到满足你条件的记录</div>& lt;/td></tr>";
}
$prev_page=$page-1;
$next_page=$page 1;
echo "<div align=''center''>";
echo " 第".$page."/".$page_count."页 ";
if ($page<=1){
echo "|第一页|";
}
else{
echo "<a href=''$PATH_INFO?page=1''>|第一页|</a>";
}
echo " ";
if ($prev_page<1){
echo "||";
}
else{
echo "<a href=''$PATH_INFO?page=$prev_page''>||</a>";
}
echo " ";
if ($next_page>$page_count){
echo "||";
}
else{
echo "<a href=''$P