php - Building an SQL query using multiple (optional) search fields -
i have form going used search through table of support tickets.
the user can search few difficult optional fields.
- date (to/from)
- ticket status
- engineer
- ticket contact
i'm wondering best way deal optional search filters. have query takes in parameters user. if user searches using both , dates query want include between. have write different query if user searches from. or query when user has not added date parameters? if status dropdown blank? query?
any clear great!
jonesy
build query in parts. start whatever constant in query, , add on more sql depending on conditions:
$query = "select ... ... [where conditions going present]"; if (isset($_post['date_from']) && isset($_post['date_to'])) { $query .= ... // query code dealing dates } if (isset($_post['status'])) { $query .= ... // deal status } // etc. // once have query built, execute $result_set = mysql_query($query);
this code skeleton, that's how construct query.
Comments
Post a Comment