//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Para ejecutar este script probablemente necesitará cambiar los siguientes parámetros.
$hostname = 'localhost';
$username = 'root';
$password = '123456';
$database = 'sakila';
$rowCount = 12; // Número de registros por página.
$pagesToShow = 3; // Número de páginas a mostrar adelante o atrás de la actual en la paginación.
@$conn = new mysqli($hostname, $username, $password, $database);
if (mysqli_connect_errno())
{
echo 'No fué posible conectarse a la base de datos ', $hostname, ': ', mysqli_connect_error(),
"
\nPor favor compruebe los parámetros de la conexión.";
exit;
}
// Obtenemos el número de registros en la tabla.
$sql = '
select count(*) filmsCount
from film
';
if (false === ($res = $conn->query($sql)))
{
echo 'Error al ejecutar la consulta "', $sql, '": ', $conn->error;
exit;
}
$row = $res->fetch_row();
$filmsCount = $row[0];
// El número de páginas que podemos mostrar.
$pagesCount = (int)ceil($filmsCount / $rowCount);
// Obtenemos el índice de la página que nos piden mostrar y nos aseguramos que esté entre 0 y $pagesCount.
$pageIndex = isset($_REQUEST['pageIndex']) ? (int)$_REQUEST['pageIndex'] : 0;
if ($pageIndex >= $pagesCount)
$pageIndex = $pagesCount - 1;
// Obtenemos unicamente los registros de la página actual.
$offset = $pageIndex * $rowCount;
$sql = "
select *
from film
order by title
limit $offset, $rowCount
";
if (false === ($res = $conn->query($sql, MYSQLI_USE_RESULT)))
{
echo 'Error al ejecutar la consulta "', $sql, '": ', $conn->error;
exit;
}
$films = array();
while ($film = $res->fetch_object())
$films[] = $film;
?>
Paginación de resultados con MySQL y PHP - Sakila Films
Paginación estilo Digg con MySQL y PHP - Sakila Films
Este es un ejemplo de paginación de resultados usando MySQL y PHP al estilo Digg. La tabla FILM de la base
de datos Sakila contiene información de películas. Esa es la información que
estamos mostrando a continuación.
Para más información sobre cómo funciona este script ingrese a Paginación estilo Digg
con MySQL y PHP en Punto Flotante
0) {
?>
- « Anterior
= $pagesCount)
$end = $pagesCount - 1;
if ($start > 0) {
for ($i = 0; $i < 2 && $i < $start; ++$i) {
?>
2) {
?>
- ...
- ...
- Siguiente »