In our site design process, often use multi-conditional query, the source of this article is a second-hand housing query example. In this case, we are going to be able to find the information that we need through a number of conditions, such as location, property type, house price, housing area and information release date. The following is the implementation process.
Query Condition Interface (abbreviated):
Query file (search.php)
First, generate the query statement:
$conn =mysql_connect ("localhost", "root", "");
$db =mysql_select_db ("Lingyun");
$query = "SELECT * from message where tradetype= '". $tradetype. "'"; Types of transactions, such as rental, sale
$SQL = $SQL. "Wuye= '". $wuye. "'";
if ($housetype! = "Unlimited") {
$query. = "&& housetype=". $housetype. "'"; Housing type, such as two rooms, one hall, three rooms, two rooms
}
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 the 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:
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 "Detailed content";
echo "";
}
}
else echo "
No records were found to meet your criteria.";
}
$prev _page= $page-1;
$next _page= $page +1;
echo "";
echo "First". $page. " /". $page _count." Page ";
if ($page <=1) {
echo "| first page |";
}
else{
echo "| first page |";
}
echo "";
if ($prev _page<1) {
echo "| prev |";
}
else{
echo "| prev |";
}
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 are no housing rental information!
";
}
echo " ";
?>
If there is any problem in the use of the process, please contact me (yk_lingyun@21cn.com), welcome correction!
"The copyright of this article is owned by the author and house Orso near net, if need to reprint, please specify 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 site design process, often use multi-conditional query, the source of this article is a second-hand housing query example. In this example, we want to achieve the ability to pass geographical location, things ...