Skip to content

Instantly share code, notes, and snippets.

@prabalsslw
Last active August 19, 2021 13:23
Show Gist options
  • Save prabalsslw/3892a17bd5debd61ce1614533e7ab1b6 to your computer and use it in GitHub Desktop.
Save prabalsslw/3892a17bd5debd61ce1614533e7ab1b6 to your computer and use it in GitHub Desktop.
Simple Query String to SQL WHERE Clause - This script will help you to simplify your search module
$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries); // GET Query String from URL
$result = array_filter($queries); // Filter empty string
$keys = array_keys($result); // Separate array key
$values = array_values($result); // Separate array value
function queryParsing($key, $value)
{
return($key." = "."'$value'"); // Build string like username = 'value'
}
$condition_stirng = array_map("queryParsing" ,$keys, $values); // Mapping key and values
$purify_string = implode(" AND ", $condition_stirng); // Add string like SQL WHERE Clause(username = 'value' AND name= 'value')
if (strripos($purify_string, "AND search_user = 'Filter'") !== false) {
$fresh_string = str_replace("AND search_user = 'Filter'",'',$purify_string); // Remove search button key and value from the string
}
else if(strripos($purify_string, "search_user = 'Filter'") !== false) {
$fresh_string = str_replace("search_user = 'Filter'",'',$purify_string); // Remove search button key and value from the string
}
/* Input:
* ?username=Test&phone=01111111111&mrid=2342242&merchant_id=3&branch_id=2&search_user=Filter
* Output:
* username = 'Test' AND phone = '01111111111' AND mrid = '2342242' AND merchant_id = '3' AND branch_id = '2'
* SQL: SELECT * FROM YOUR_TABLE WHERE username = 'Test' AND phone = '01111111111' AND mrid = '2342242' AND merchant_id = '3' AND branch_id = '2'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment