$updated_session = [SessionOperator::SORT => $sort]; } else { HelperOperator::redirectTo("../views/search_view.php"); return; } } } $cats = getCatIdAndType($searchCategory); // Set up pagination object $total = QueryOperator::countFoundAuctions(buildQuery($searchString, $cats, null)); $page = isset($_GET["page"]) ? $_GET["page"] : 1; $page = $page <= $total ? $page : 1; $per_page = 15; $pagination = new Pagination($page, $per_page, $total); // Get paginated search results $catsAndAuctions = QueryOperator::searchAuctions(buildQuery($searchString, $cats, $sort, $per_page, $pagination->offset())); // Update search sessions $updated_session = array_merge([SessionOperator::SEARCH_RESULT => $catsAndAuctions], $updated_session); $updated_session = array_merge([SessionOperator::SEARCH_PAGINATION => $pagination], $updated_session); SessionOperator::setSearch($updated_session); // Return back to search page HelperOperator::redirectTo("../views/search_view.php"); function buildQuery($searchString, $searchCategory, $sortOption, $limit = null, $offset = null) { $query = null; // Prepare count query if (is_null($limit) && is_null($offset)) { $query = "SELECT COUNT(*) "; } else { $query = "SELECT auctions.auctionId, quantity, startPrice, reservePrice, startTime,\n endTime, itemName, itemBrand, itemDescription, items.image, auctions.views,\n item_categories.categoryName as subCategoryName, superCategoryName,\n item_categories.superCategoryId, item_categories.categoryId,\n conditionName, countryName, COUNT(DISTINCT (bids.bidId)) AS numBids,\n COUNT(DISTINCT (auction_watches.watchId)) AS numWatches,\n MAX(bids.bidPrice) AS highestBid,\n case\n when MAX(bids.bidPrice)is not null THEN MAX(bids.bidPrice)\n else startPrice\n end AS currentPrice "; }