I've already been able to construct a mySQL query using select boxes. Starting with ajax to PHP like so.
html/js (relevant parts)
var call = true;
switch(parseInt($(this).val())) {
case 1:
cat_code ="ager";
sortv = "database_percent";
sorto = "asc";
break;
case 2:
cat_code ="ager";
sortv = "database_percent";
sorto = "desc";
break;
default:
cat_code ="ager";
sortv = "value";
sorto = "asc";
break;
}
if(call) {
$.ajax({
url: 'all_get_2.php',
type: 'GET',
dataType: 'JSON',
data: {cat_code: cat_code, sortvalue: sortv ,sortorder:sorto},
success: function(data) {
//stuff
}
})
PHP (relevant parts...just an example)
$whereCategory = isset($_GET['cat_code'])? "{$_GET['cat_code']}" : '';
$sortvalue = isset($_GET['sortvalue'])? "{$_GET['sortvalue']}" : '';
$sortorder = isset($_GET['sortorder'])? "{$_GET['sortorder']}" : '';
$sql = "select count(guid) from full_db2 where '{$whereCategory}' = '{$sortvalue}'' and example = '{$sortorder};";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($result);
All's well. I mixed up some variables in this illustration, but that's not the issue. This returns the count for the attributes that the user selected from the selects. The selects are dynamically generated based on what the user selected in the previous select.
What if I wanted users to be able to select a bunch more attributes to add to the query. So, for example, instead of finding the count for the number of entries in the database (users) who's age is more than 40, they could add on more to this like users with age greater than 40 and married and have children.
I considered basically adding an option to add another set of select boxes (e.g., click a "+" and new set appears), but I quickly realized that I wouldn't be able to figure out how many selects were out there and a logical way to create a query.
What's the best possible way to allow users to select a (theoretically) unlimited number of attributes to construct in an single mySQL query?