An example of a simple implementation of a multiple-condition query

Source: Internet
Author: User
In our website design process, often use a multiple conditions query, the source of this article is a second-hand housing query example. In this case, we need to be able to find the information we need through our geographical location, property type, house price, house area and information release date. The following is the implementation process.
Query Criteria Interface (abbreviated):
Query file (search.php)
First, generate query statements:
?
$conn =mysql_connect ("localhost", "root", "");
$db =mysql_select_db ("Lingyun");
$query = "SELECT * from message where tradetype= '". $tradetype. "'"; Type of transaction, such as renting, selling
$SQL = $SQL. "Wuye= '". $wuye. "'";
if ($housetype!= "unlimited") {
$query. = "&& housetype= '". $housetype. "'"; Type of house, such as two bedroom, one hall, three rooms and two halls
}
if ($degree!= "unlimited") {
$query. = "&& degree= '". $degree. "'"; New and Old degree
}
if ($wuye!= "unlimited") {
$query. = "&& wuye= '". $wuye. "'"; Property types such as housing, shops
}
if ($price 2!= "") {
Switch ($price 1) {
Case "Greater than":
$query. = "&& price> '". $price 2. "'"; Price
Break
Case "equals":
$query. = "&& price= '". $price 2. "'";
Break
Case "less than":
$query. = "&& price< '". $price 2. "'";
Break
}
}
if ($area 2!= "") {
Switch ($area 1) {
Case "Greater than":
$query. = "&& area> '". $area 2. "'"; Area
Break
Case "equals":
$query. = "&& area= '". $area 2. "'";
Break
Case "less than":
$query. = "&& area< '". $area 2. "'";
Break
}
}
Switch ($pubdate) {//release date
Case "Within this week":
$query. = "&& to_days (Now ())-to_days (date) <=7";
Break
Case "within one months":
$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;
}
?>

Second, output query results:
<?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= ' height= '" ></td> ';
echo "<td width= ' 540 ' height= ' > $myrow [id] $myrow [tradetype] $myrow [address] $myrow [Wuye] ($myrow [Housetype]) <font style= ' font-size:9pt ' >[$myrow [date]]</font> ";
echo "</td>";
echo "<td width= ' height= ' ><a href= ' view_d.php?code= $myrow [code] ' target= ' _blank ' > Details </a> </td> ";
echo "</tr>";
}
}
else echo "<tr><td><div align= ' center ' ><br><br> did not find a record to meet your requirements </div></td></tr> ";
}

$prev _page= $page-1;
$next _page= $page +1;
echo "<div align= ' center ' >";
echo "section" $page. " /". $page _count." Page ";
if ($page <=1) {
echo "| first page |";
}
else{
echo "<a href= ' $PATH _info?page=1 ' >| first page |</a>";
}
echo "";
if ($prev _page<1) {
echo "| prev |";
}
else{
echo "<a href= ' $PATH _info?page= $prev _page ' >| prev |</a>";
}
echo "";
if ($next _page> $page _count) {
echo "| next page |";
}
else{
echo "<a href= ' $PATH _info?page= $next _page ' >| next page |</a>";
}
echo "";
if ($page >= $page _count) {
echo "| last page |";
}
else{
echo "<a href= ' $PATH _info?page= $page _count ' >| last page |</a>";
}
echo "</div>";
}
else{
echo "<p align= ' center ' > now has no housing leasing information!</p>";
}
echo "?>
</table>

If there is any problem in the use of the process, please contact me (yk_lingyun@21cn.com), welcome correction!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.