简体   繁体   中英

How to filter by multiple fields in MySQL/PHP

I'm writing a filter/sorting feature for an application right now that will have text fields above each column. As the user types in each field, requests will be sent to the back-end for sorting. Since there are going to be around 6 text fields, I was wondering if there's a better way to sort instead of using if statements to check for each variable, and writing specific queries if say all fields were entered, just one, or just two fields, etc.

Seems like there would be a lot of if statements. Is there a more intuitive way of accomplishing this?

Thanks!

Any initial data manipulation, such as sorting, is usually done by the database engine.

Put an ORDER BY clause in there, unless you have a specific reason the sorting needs done in the application itself.


Edit: You now say that you want to filter the data instead. I would still do this at the database level. There is no sense in sending a huge dataset to PHP, just for PHP to have to wade through it and filter out data there. In most cases, doing this within MySQL will be far more efficient than what you can build in PHP.

Since there are going to be around 6 text fields, I was wondering if there's a better way to sort instead of using if statements to check for each variable

Definitely NO.

First, nothing wrong in using several if's in order.
Trust me - I myself being a huge fan of reducing repetitions of code, but consider these manually written blocks being the best solution.
Next, although there can be a way to wrap these condition ns some loop, most of time different conditions require different treatment.

however, in your next statements you are wrong:

and writing specific queries

you need only one query

Seems like there would be a lot of if statements.

why? no more than number of fields you have.

here goes a complete example of custom search query building code:

$w     = array();
$where = '';

if (!empty($_GET['rooms']))     $w[]="rooms='".mesc($_GET['rooms'])."'";
if (!empty($_GET['space']))     $w[]="space='".mesc($_GET['space'])."'";
if (!empty($_GET['max_price'])) $w[]="price < '".mesc($_GET['max_price'])."'";

if (count($w)) $where="WHERE ".implode(' AND ',$w);
$query="select * from table $where"; 

the only fields filled by the user going to the query.

the ordering is going to be pretty the same way.

  • mesc is an abbreviation for the mysql_real_escape_string or any other applicable database-specific string escaping function
select * from Users

order by Creadted desc, Name asc, LastName desc, Status asc

And your records will be sorted by order from query.

First by Created desc, then by Name asc and so on.

But from your question I can see that you are searching for filtering results.

So to filter by multiple fileds just append your where, or if you are using any ORM you can do it through object methods.

But if its simple you can do it this way

$query = "";
foreach($_POST['grid_fields'] as $key => $value)
{
   if(strlen($query) > 0)
         $query .= ' and '
   $query .= sprintf(" %s LIKE '%s' ", mysql_real_escape_string($key), '%' .mysql_real_escape_string($value) .'%');  
}
if(strlen($query) > 0)
   $original_query .= ' where ' . $query;

this could help you to achieve your result.

No. You cannot avoid the testing operations when sorting the set, as you have to compare the elements in the set in same way. The vehicle for this is an if statement.

Could you take a look at this?

WHERE (ifnull(@filter1, 1) = 1 or columnFilter1 = @filter1)
  and (ifnull(@filter2, 1) = 1 or columnFilter2 = @filter2)
  and (ifnull(@filter3, 1) = 1 or columnFilter3 = @filter3)
  and (ifnull(@filter4, 1) = 1 or columnFilter4 = @filter4)
  and (ifnull(@filter5, 1) = 1 or columnFilter5 = @filter5)
  and (ifnull(@filter6, 1) = 1 or columnFilter6 = @filter6)

Please let me know if I'm misunderstanding your question.. It's not like an IF statement batch, and is pretty lengthy, but what do you think?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM