I am building a website where i need to build a mysql query based on user input, where not only the values, but also the query itself need to change. What i have this far is a conditional if-else solution which works, but is very messy...
example:
public function getUsers($options){
$query = "SELECT * FROM Users"; // basic query
if($options->getAdminsOnly()){
$query .=" WHERE type='admin' ";
}
if($options->getOrderBy() !=null){
$query .=" ORDER BY ".$options->getOrderBy();
}
if($options->groupBy() !=null){
$query .=" GROUP BY ".$options->groupBy();
}
if($options->getLimit() !=null){
$query .=" LIMIT ".$options->getLimit();
}
if($options->getOffset() !=null){
$query .=" OFFSET ".$options->getOffset();
}
....
....
}
This gets even messier in case of joins etc... Is there a cleaner way to do this?