Solution: PHP Commodity Conditional filtering How did you do it?

Source: Internet
Author: User

Solution: PHP Commodity Conditional filtering How did you do it? Share | 2013-09-25 13:43 chenhang607 | browse 2,756 times resource Sharing
To ask for ideas or methods, preferably some code
2013-09-25 14:14 The questioner adopted
PHP conditionally filter the function of the product, or relatively simple.
In fact, according to different conditions to form SQL query conditions, from the database to find out the different products.
As an example:
Users can be based on the price range, by brand, according to the name of the product of a comprehensive query.
Then, after the user has selected these query conditions (perhaps these 3 conditions are set, it is possible to set only 2 of them), submitted to the server side, the service side program receives the user's query criteria, start to assemble SQL query statements, and finally execute the assembled SQL query statement, return the results to the user.

code example:
User-Submitted queries:
Price= ' 0-1000 '; 0-1000 USD by Price range

brandid=20; The requirement brand must be the ID number 20 (assuming the brand with ID number 20, called "Western Data")
Productname= ' green plate '; The product name only has 2 characters to make the fuzzy query

After the user points to the query button, depending on the program, it may be a get or post method to send the query request, now assume that the post method.

Then, in the server-side program, you receive a query request from the user:
$price =$_post[' price '];
$price _arr=explode ('-', $price); This is to split the price range by-number group, which is convenient for assembling SQL query statements.

$brandid =$_post[' Brandid '];
$productname =$_post[' ProductName '];

Of course, after the above data acquisition, but also to carry out the corresponding security aspects of the inspection and processing, here first skip.

Next, start assembling the SQL query statement:

$sqlexp = ";
The following group starts the Assembly of SQL query criteria based on the query criteria set by the user

if (!empty ($price)) {
$sqlexp. = ' and (price>= '. $price _arr[0]. ' and price<= '. $price _arr[1];

}
if (!empty ($brandid)) {
$sqlexp. = ' and brandid= '. $brandid;
}
if (!empty ($productname)) {
$sqlexp. = "and InStr (ProductName, ' $productname ') >0";
}

Execute SQL query below

$result =mysql_query ("SELECT * from Product where state=1". $sqlexp);

After the above query is completed, the query results are returned to the user, the user to filter the product according to the conditions of the function.
Ask:
Well, back to the address bar parameters, splicing where condition, this is good, not do not know, how the front page query conditions above a link construction address.
Chase Answer:
In the front-end pages, it is common to use form forms instead of a link.
<form name= "Search_form" method= "post" action= "search.php" >
Price range: <input type= "text" name= "Prices"/>
Brand: <select name= "Brandid" >
<option value= "" > Please select </option>
<option value= "1" > Seagate </option>
<option value= > Western Data </option>
<option value= > Hitachi </option>
</select>
Product Name: <input type= "text" name= "ProductName"/>
<input type= "Submit" Name= "Searchbtn" value= "Start query"/>

Solution: PHP Commodity Conditional filtering How did you do it?

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.