PHP implementation of multi-conditional query Instance Code _php tutorial

Source: Internet
Author: User
Query file (search.php)
first, generate the query statement:

Copy CodeThe code is as follows:
$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:
Copy CodeThe 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 "";
echo "";
echo "$myrow [id] $myrow [tradetype] $myrow [address] $myrow [Wuye] ($myrow [Housetype]) [$myrow [Date]] ";
echo "";
echo "Detailed content";
echo "";
}
}
else echo "

No records found to meet your criteria & lt;/td>";
}
$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 " ";
?>

http://www.bkjia.com/PHPjc/322266.html www.bkjia.com true http://www.bkjia.com/PHPjc/322266.html techarticle query file (search.php) One, generate query statements: Copy code code as follows:? $conn =mysql_connect ("localhost", "root", ""), $db =mysql_select_db ("Lingyun "); $query = "Select * Fr ...

  • 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.