Add server-side pagination to medicos list (25 per page)
list.php now accepts page/limit params and returns total count. View shows X-Y de N label, page buttons with ellipsis for large ranges, and prev/next arrows. Search resets to page 1; save/delete stay on current page. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
00725ccd11
commit
0cef678848
@@ -6,21 +6,35 @@ header('Content-Type: application/json');
|
||||
|
||||
$pdo = Database::getInstance()->getConnection();
|
||||
$buscar = trim($_GET['q'] ?? '');
|
||||
$page = max(1, (int)($_GET['page'] ?? 1));
|
||||
$limit = max(1, min(200, (int)($_GET['limit'] ?? 25)));
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
$where = '';
|
||||
$params = [];
|
||||
|
||||
if ($buscar !== '') {
|
||||
$like = '%' . $buscar . '%';
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT id, codigo, nombres, apellidos, telefonos, email, cod_especialidad, docidmedico, activo
|
||||
FROM medicos
|
||||
WHERE nombres LIKE ? OR apellidos LIKE ? OR codigo LIKE ? OR docidmedico LIKE ?
|
||||
ORDER BY apellidos, nombres"
|
||||
);
|
||||
$stmt->execute([$like, $like, $like, $like]);
|
||||
} else {
|
||||
$stmt = $pdo->query(
|
||||
"SELECT id, codigo, nombres, apellidos, telefonos, email, cod_especialidad, docidmedico, activo
|
||||
FROM medicos ORDER BY apellidos, nombres"
|
||||
);
|
||||
$like = '%' . $buscar . '%';
|
||||
$where = "WHERE nombres LIKE ? OR apellidos LIKE ? OR codigo LIKE ? OR docidmedico LIKE ?";
|
||||
$params = [$like, $like, $like, $like];
|
||||
}
|
||||
|
||||
echo json_encode(['ok' => true, 'data' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
|
||||
$countStmt = $pdo->prepare("SELECT COUNT(*) FROM medicos $where");
|
||||
$countStmt->execute($params);
|
||||
$total = (int)$countStmt->fetchColumn();
|
||||
|
||||
$dataStmt = $pdo->prepare(
|
||||
"SELECT id, codigo, nombres, apellidos, telefonos, email, cod_especialidad, docidmedico, activo
|
||||
FROM medicos $where
|
||||
ORDER BY apellidos, nombres
|
||||
LIMIT $limit OFFSET $offset"
|
||||
);
|
||||
$dataStmt->execute($params);
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'data' => $dataStmt->fetchAll(PDO::FETCH_ASSOC),
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user