<?php
-// database connection settings
-$server = 'localhost';
-$username = 'demo';
-$password = '';
-$database = 'demo'; // 'gridfiltersdemo' in sample sql
+function getDB() {
+ $dbFile = "filter-demo.db";
+ $hasDB = file_exists($dbFile);
-// connect to database
-mysql_pconnect($server, $username, $password) or die("Could not connect");
-mysql_select_db($database) or die("Could not select database");
+ $db = new SQLiteDatabase($dbFile);
+ if (!$hasDB) {
+ $db->query(readCreateSql());
+ }
+ return $db;
+}
+
+function readCreateSql() {
+ $filename = "grid-demo.sql";
+ $file = fopen($filename, 'r');
+ $data = fread($file, filesize($filename));
+ fclose($file);
+ return $data;
+}
// collect request parameters
$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
-$count = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 20;
+$count = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 50;
$sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '';
$dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : 'ASC';
$filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
$filters = json_decode($filters);
}
-// initialize variables
$where = ' 0 = 0 ';
$qs = '';
$where .= $qs;
}
-// query the database
$query = "SELECT * FROM demo WHERE ".$where;
if ($sort != "") {
$query .= " ORDER BY ".$sort." ".$dir;
}
$query .= " LIMIT ".$start.",".$count;
-$rs = mysql_query($query);
-$total = mysql_query("SELECT COUNT(id) FROM demo WHERE ".$where);
-$total = mysql_result($total, 0, 0);
-
-$arr = array();
-while($obj = mysql_fetch_object($rs)) {
- $arr[] = $obj;
+$db = getDB();
+$count = $db->singleQuery("SELECT COUNT(id) FROM demo WHERE ".$where);
+$result = $db->query($query);
+$rows = Array();
+while($row = $result->fetch(SQLITE_ASSOC)) {
+ array_push($rows, $row);
}
+echo json_encode(Array(
+ "total"=>$count,
+ "data"=>$rows
+));
-// return response to client
-echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';
\ No newline at end of file
+?>
\ No newline at end of file