-$where = " 0 = 0 ";
-if (is_array($filter)) {
- for ($i=0;$i<count($filter);$i++){
- switch($filter[$i]['data']['type']){
- case 'string' : $qs .= " AND ".$filter[$i]['field']." LIKE '%".$filter[$i]['data']['value']."%'"; Break;
- case 'list' :
- if (strstr($filter[$i]['data']['value'],',')){
- $fi = explode(',',$filter[$i]['data']['value']);
- for ($q=0;$q<count($fi);$q++){
- $fi[$q] = "'".$fi[$q]."'";
- }
- $filter[$i]['data']['value'] = implode(',',$fi);
- $qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
- }else{
- $qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
- }
- Break;
- case 'boolean' : $qs .= " AND ".$filter[$i]['field']." = ".($filter[$i]['data']['value']); Break;
- case 'numeric' :
- switch ($filter[$i]['data']['comparison']) {
- case 'eq' : $qs .= " AND ".$filter[$i]['field']." = ".$filter[$i]['data']['value']; Break;
- case 'lt' : $qs .= " AND ".$filter[$i]['field']." < ".$filter[$i]['data']['value']; Break;
- case 'gt' : $qs .= " AND ".$filter[$i]['field']." > ".$filter[$i]['data']['value']; Break;
- }
- Break;
- case 'date' :
- switch ($filter[$i]['data']['comparison']) {
- case 'eq' : $qs .= " AND ".$filter[$i]['field']." = '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'"; Break;
- case 'lt' : $qs .= " AND ".$filter[$i]['field']." < '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'"; Break;
- case 'gt' : $qs .= " AND ".$filter[$i]['field']." > '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'"; Break;
- }
- Break;
- }
- }
- $where .= $qs;
+// connect to database
+mysql_pconnect($server, $username, $password) or die("Could not connect");
+mysql_select_db($database) or die("Could not select database");
+
+// collect request parameters
+$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
+$count = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 20;
+$sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
+$dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : 'ASC';
+$filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
+
+// GridFilters sends filters as an Array if not json encoded
+if (is_array($filters)) {
+ $encoded = false;
+} else {
+ $encoded = true;
+ $filters = json_decode($filters);
+}
+
+// initialize variables
+$where = ' 0 = 0 ';
+$qs = '';
+
+// loop through filters sent by client
+if (is_array($filters)) {
+ for ($i=0;$i<count($filters);$i++){
+ $filter = $filters[$i];
+
+ // assign filter data (location depends if encoded or not)
+ if ($encoded) {
+ $field = $filter->field;
+ $value = $filter->value;
+ $compare = isset($filter->comparison) ? $filter->comparison : null;
+ $filterType = $filter->type;
+ } else {
+ $field = $filter['field'];
+ $value = $filter['data']['value'];
+ $compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
+ $filterType = $filter['data']['type'];
+ }
+
+ switch($filterType){
+ case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; Break;
+ case 'list' :
+ if (strstr($value,',')){
+ $fi = explode(',',$value);
+ for ($q=0;$q<count($fi);$q++){
+ $fi[$q] = "'".$fi[$q]."'";
+ }
+ $value = implode(',',$fi);
+ $qs .= " AND ".$field." IN (".$value.")";
+ }else{
+ $qs .= " AND ".$field." = '".$value."'";
+ }
+ Break;
+ case 'boolean' : $qs .= " AND ".$field." = ".($value); Break;
+ case 'numeric' :
+ switch ($compare) {
+ case 'eq' : $qs .= " AND ".$field." = ".$value; Break;
+ case 'lt' : $qs .= " AND ".$field." < ".$value; Break;
+ case 'gt' : $qs .= " AND ".$field." > ".$value; Break;
+ }
+ Break;
+ case 'date' :
+ switch ($compare) {
+ case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; Break;
+ case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; Break;
+ case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; Break;
+ }
+ Break;
+ }
+ }
+ $where .= $qs;