3 // database connection settings
7 $database = 'demo'; // 'gridfiltersdemo' in sample sql
10 mysql_pconnect($server, $username, $password) or die("Could not connect");
11 mysql_select_db($database) or die("Could not select database");
13 // collect request parameters
14 $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
15 $count = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 20;
16 $sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
17 $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : 'ASC';
18 $filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
20 // GridFilters sends filters as an Array if not json encoded
21 if (is_array($filters)) {
25 $filters = json_decode($filters);
28 // initialize variables
32 // loop through filters sent by client
33 if (is_array($filters)) {
34 for ($i=0;$i<count($filters);$i++){
35 $filter = $filters[$i];
37 // assign filter data (location depends if encoded or not)
39 $field = $filter->field;
40 $value = $filter->value;
41 $compare = isset($filter->comparison) ? $filter->comparison : null;
42 $filterType = $filter->type;
44 $field = $filter['field'];
45 $value = $filter['data']['value'];
46 $compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
47 $filterType = $filter['data']['type'];
51 case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; Break;
53 if (strstr($value,',')){
54 $fi = explode(',',$value);
55 for ($q=0;$q<count($fi);$q++){
56 $fi[$q] = "'".$fi[$q]."'";
58 $value = implode(',',$fi);
59 $qs .= " AND ".$field." IN (".$value.")";
61 $qs .= " AND ".$field." = '".$value."'";
64 case 'boolean' : $qs .= " AND ".$field." = ".($value); Break;
67 case 'eq' : $qs .= " AND ".$field." = ".$value; Break;
68 case 'lt' : $qs .= " AND ".$field." < ".$value; Break;
69 case 'gt' : $qs .= " AND ".$field." > ".$value; Break;
74 case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; Break;
75 case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; Break;
76 case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; Break;
85 $query = "SELECT * FROM demo WHERE ".$where;
87 $query .= " ORDER BY ".$sort." ".$dir;
89 $query .= " LIMIT ".$start.",".$count;
91 $rs = mysql_query($query);
92 $total = mysql_query("SELECT COUNT(id) FROM demo WHERE ".$where);
93 $total = mysql_result($total, 0, 0);
96 while($obj = mysql_fetch_object($rs)) {
100 // return response to client
101 echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';