Home >Backend Development >PHP Tutorial >PHP implementation of multi-condition query example code_PHP tutorial

PHP implementation of multi-condition query example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:35:20919browse

Query file (search.php)
1. Generate query statement:

Copy code The code is as follows:

$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("lingyun");
$query=" select * from message where tradetype='".$tradetype."'"; //Transaction type, such as renting, selling
$SQL=$SQL . "wuye='" . $wuye . "'";
if($housetype!="No limit"){
$query.=" && housetype='".$housetype."'"; //House type, such as two bedrooms and one living room, three bedrooms and two living rooms
}
if($degree!="No limit"){
$query.=" && degree='".$degree."'"; //New and old degree
}
if ($wuye!="No limit"){
$query.=" && wuye='".$wuye."'"; //Property type such as housing, shop
}
if($ price2!=""){
switch($price1){
case "greater than":
$query.=" && price>'".$price2."'"; //Price
break;
case "equal to":
$query.=" && price='".$price2."'";
break;
case "less than":
$query .=" && price<'".$price2."'";
break;
}
}
if($area2!=""){
switch($area1) {
case "greater than":
$query.=" && area>'".$area2."'"; //area
break;
case "equal to":
$ query.=" && area='".$area2."'";
break;
case "less than":
$query.=" && area<'".$area2."'" ;
break;
}
}
switch($pubdate){ //Publish date
case "Within this week":
$query.=" && TO_DAYS(NOW( )) - TO_DAYS(date)<=7";
break;
case "within one month":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)< =30";
break;
case "within three months":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=91";
break ;
case "within six months":
$query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=183";
break;
}
if($address!=""){
$query.=" && address like '%$address%'"; //Address
}
if(!$page){
$ page=1;
}
?>


2. Output query results:
Copy the code The code is as follows:

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>";
echo "$myrow[id] $myrow[tradetype] $myrow[address] $myrow[wuye]($myrow[housetype ])[$myrow[date]]";
echo "";
echo "Details" ;
echo "";
}
}
else echo "


No records meeting your criteria were found
& lt;/td>";
}
$ prev_page=$page-1;
$next_page=$page 1;
echo "
";
echo "".$page."/".$ page_count."page ";
if ($page<=1){
echo "|First page|";
}
else{
echo "|First page|";
}
echo " ";
if ($prev_page<1){
echo "| Previous page|";
}
else{
echo "|Previous page|";
}
echo " ";
if ($next_page>$page_count){
echo "|next page|";
}
else{
echo "|Next page|";
}
echo " ";
if ($page>=$page_count){
echo "|last page|";
}
else{
echo "|last page|}
echo "
";
}
else{
echo "

There is no rental information yet!

";
}
echo "
";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322266.htmlTechArticleQuery file (search.php) 1. Generate query statement: Copy the code as follows: ? $conn=mysql_connect( "localhost","root",""); $db=mysql_select_db("lingyun"); $query="select * fr...