function get_products($page, $per_page, $short = false) { $start = abs($page * $per_page); $fields = $short ? 'product.id' : '*'; if (isset($_GET['sort'])) { $sort_query = $_GET['sort']; } elseif (!empty($_POST['sort'])) { $sort_query = $_POST['sort']; } else { $sort_query = 'id_asc'; } $cache_key = build_cache_key($page, $sort_query); $result = get_cache($cache_key); if (!$result) { $sort_data = get_sort($sort_query); $sort = $sort_data['sort']; $sort_field = $sort_data['sort_field']; if ($sort_field == 'id') { $q = 'select ' . $fields . ' from product JOIN (SELECT ' . $sort_field . ' FROM product ' . $sort . ' LIMIT ' . $start . ',' . $per_page . ') as b ON b.id = product.id'; } elseif ($sort_field == 'price') { $q = 'select ' . $fields . ' from product JOIN (SELECT id, price FROM product ' . $sort . ' LIMIT ' . $start . ',' . $per_page . ') as b ON b.id = product.id'; } $res = query($q); } else { $q = 'select ' . $fields . ' from product where id in(' . $result . ');'; $res = query($q); } return $res; }
function save_page_data($page, $sort, $per_page) { $cache_key = build_cache_key($page, $sort); $result = get_cache($cache_key); if (!$result) { $data = get_products($page, $per_page, true); $ids = ''; foreach ($data as $product) { $ids .= $product[0] . ','; } set_cache($cache_key, substr($ids, 0, -1)); } }