There is a filter with a lot of data:

  1. price: minimum, maximum
  2. number of rooms: studio, 1,2,3,4 +
  3. finishing: without finishing, draft, fair
  4. delivery of the object: 2017,2018,2019,2020
  5. Payment options: installment plan, mortgage, mat capital
  6. apartment area: min, max
  7. class: economy, comfort, business, premium

Each filter value may or may not be selected. The database stores data in various interrelated tables. Actually, the question is how to take into account all the parameters when creating the SQL query?

  • Sample data in different interrelated tables? And what exactly do you want to see in the SQL query? Get information on each parameter, or send to the database the order collected from the parameters? - Dan the Hat
  • one
    Check each data for emptiness and in accordance with this data form the sql query to your database - Oleg Shleif
  • It is necessary to make a sample of objects that fit the selected parameters and display to the user, checking each for emptiness is not a problem, but how to make a request considering not empty parameters? - Danil Shuvaev
  • one
    @ DanilShuvaev appends a where clause to a condition in the form: where 1=1 and (дальше условия, например:) rooms in (1, 2, 3, 4) and class in ('econom', 'comfort') and ... - Denis

1 answer 1

Include the additional condition " AND (OR) WHERE " depending on the specified data. Similarly implemented in many ORMs (Doctrine for example), you can make your own method using this example. Total we get: The class forming the query, where all WHERE conditions depend on the $where property, which is in turn modified by the methods orWhere($condition) andWhere($condition) total query inside the newly created query builder will look like

 $query = "SELECT * FROM table " . $this->where . " LIMIT 10000";