$page, 'per_page' => $perPage, 'sort' => $sortMap[$sort], 'order' => $order, 'search' => $search ? mb_substr($search, 0, 100) : '', ]; } /** * Spusti COUNT + SELECT dotazy s pagination a vrati vysledek. * * @param PDO $pdo * @param string $countSql - COUNT(*) dotaz * @param string $dataSql - SELECT dotaz (bez LIMIT/OFFSET) * @param array $params - parametry pro prepared statement * @param array{page: int, per_page: int, sort: string, order: string} $pagination * @return array{items: array, pagination: array} */ public static function paginate( PDO $pdo, string $countSql, string $dataSql, array $params, array $pagination ): array { $page = $pagination['page']; $perPage = $pagination['per_page']; $stmt = $pdo->prepare($countSql); $stmt->execute($params); $total = (int) $stmt->fetchColumn(); $offset = ($page - 1) * $perPage; $totalPages = (int) ceil($total / $perPage); $sql = "{$dataSql} LIMIT {$perPage} OFFSET {$offset}"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $items = $stmt->fetchAll(); return [ 'items' => $items, 'pagination' => [ 'total' => $total, 'page' => $page, 'per_page' => $perPage, 'total_pages' => $totalPages, ], ]; } }