4 $dbFile = "filter-demo.db";
5 $hasDB = file_exists($dbFile);
7 $db = new SQLiteDatabase($dbFile);
9 $db->query(readCreateSql());
14 function readCreateSql() {
15 $filename = "grid-demo.sql";
16 $file = fopen($filename, 'r');
17 $data = fread($file, filesize($filename));
22 // collect request parameters
23 $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
24 $count = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 50;
25 $sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
26 $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : 'ASC';
27 $filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
29 // GridFilters sends filters as an Array if not json encoded
30 if (is_array($filters)) {
34 $filters = json_decode($filters);
40 // loop through filters sent by client
41 if (is_array($filters)) {
42 for ($i=0;$i<count($filters);$i++){
43 $filter = $filters[$i];
45 // assign filter data (location depends if encoded or not)
47 $field = $filter->field;
48 $value = $filter->value;
49 $compare = isset($filter->comparison) ? $filter->comparison : null;
50 $filterType = $filter->type;
52 $field = $filter['field'];
53 $value = $filter['data']['value'];
54 $compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
55 $filterType = $filter['data']['type'];
59 case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; Break;
61 if (strstr($value,',')){
62 $fi = explode(',',$value);
63 for ($q=0;$q<count($fi);$q++){
64 $fi[$q] = "'".$fi[$q]."'";
66 $value = implode(',',$fi);
67 $qs .= " AND ".$field." IN (".$value.")";
69 $qs .= " AND ".$field." = '".$value."'";
72 case 'boolean' : $qs .= " AND ".$field." = ".($value); Break;
75 case 'eq' : $qs .= " AND ".$field." = ".$value; Break;
76 case 'lt' : $qs .= " AND ".$field." < ".$value; Break;
77 case 'gt' : $qs .= " AND ".$field." > ".$value; Break;
82 case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; Break;
83 case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; Break;
84 case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; Break;
92 $query = "SELECT * FROM demo WHERE ".$where;
94 $query .= " ORDER BY ".$sort." ".$dir;
96 $query .= " LIMIT ".$start.",".$count;
99 $count = $db->singleQuery("SELECT COUNT(id) FROM demo WHERE ".$where);
100 $result = $db->query($query);
102 while($row = $result->fetch(SQLITE_ASSOC)) {
103 array_push($rows, $row);
105 echo json_encode(Array(