/** * entry point */ function init() { try { $statuses = $_POST["statuses"]; $sort = ""; $filterArr = array(); $filter = ""; $paging = ""; $pagingStatus = null; $query = ""; $html = ""; //error_log(urldecode($_POST["statuses"])); if (isset($statuses)) { //statuses => array $statuses = json_decode(urldecode($statuses)); foreach ($statuses as $key => $value) { switch ($value->action) { case "paging": $pagingStatus = $value; break; case "filter": $filter .= getFilterQuery($value, $filter); break; case "sort": $sort = getSortQuery($value); break; } } //connect to SQLite database $db = new PDO("sqlite:jplist.db"); //increases sqlite performance by turning syncing off $db->exec("pragma synchronous = off;"); //count database items for pagination $query = "SELECT count(*) FROM Item " . $filter . " " . $sort; $count = $db->query($query)->fetchColumn(); //init pagination query if ($pagingStatus) { $paging = getPagingQuery($pagingStatus, $count); } //init query with sort and filter $query = "SELECT title, desc, img, likes, keyword1, keyword2 FROM Item " . $filter . " " . $sort . " " . $paging; //select items $items = $db->query($query); foreach ($items as $item) { $html .= getHTML($item); } //init array for json data //$json_arr = array("html" => utf8_encode($html), "data" => array("count" => $count), "query"=> $query); //print html echo getHTMLWrapper($html, $count); //close the database connection $db = NULL; } } catch (PDOException $ex) { print 'Exception: ' . $ex->getMessage(); } }
/** * entry point */ function init() { try { //connect to database $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD); $preparedParams = array(); $statuses = $_POST["statuses"]; $html = ""; $pagingStatus = null; $filter = ""; $sort = ""; $query = ""; if (isset($statuses)) { //statuses => array $statuses = json_decode(urldecode($statuses)); foreach ($statuses as $key => $value) { switch ($value->action) { case "paging": $pagingStatus = $value; break; case "filter": $filter .= getFilterQuery($value, $filter, $preparedParams); break; case "sort": $sort = getSortQuery($value, $preparedParams); break; } } } //count database items for pagination $query = "SELECT count(*) FROM Item " . $filter . " " . $sort; if (count($preparedParams) > 0) { $stmt = $db->prepare($query); //error_log(print_r($preparedParams, true)); $stmt->execute($preparedParams); $count = $stmt->fetchColumn(); } else { $count = $db->query($query)->fetchColumn(); } //init pagination query if ($pagingStatus) { $paging = getPagingQuery($pagingStatus, $count, $preparedParams); } //init query with sort and filter $query = "SELECT title, description, image, likes, keyword1, keyword2 FROM Item " . $filter . " " . $sort . " " . $paging; if (count($preparedParams) > 0) { $stmt = $db->prepare($query); $stmt->execute($preparedParams); $items = $stmt->fetchAll(); } else { $items = $db->query($query); } foreach ($items as $item) { $html .= getHTML($item); } //print html echo getHTMLWrapper($html, $count); //close the database connection $db = NULL; } catch (PDOException $ex) { print 'Exception: ' . $ex->getMessage(); } }