Last active
August 29, 2015 14:26
-
-
Save porfidev/2822e9a620545a55d0fa to your computer and use it in GitHub Desktop.
Paginador PHP con PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: elporfirio | |
* Date: 01/08/15 | |
* Time: 8:35 | |
*/ | |
function conexionBase(){ | |
#Datos para conexión | |
$domain = "localhost"; | |
$database = "mexico_db"; | |
$user = "homestead"; | |
$password = "secret"; | |
#Conectarse a la base de datos | |
try { | |
return new PDO( | |
'mysql:host=' . $domain . ';dbname=' . $database .';port=3306', | |
$user, | |
$password, | |
array( | |
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) | |
); | |
} catch (PDOException $ex){ | |
echo "<strong>Error de Conexión: </strong>" . $ex->getMessage() . "<br>"; | |
die(); | |
} | |
} | |
function consultarEstados($conexion, $inicio = 0, $cantidad = 5){ | |
#Consultar datos | |
try{ | |
$query = 'SELECT * FROM estados limit :inicio, :cantidad'; | |
$stmt = $conexion->prepare($query); | |
$stmt->bindParam(':inicio', $inicio, PDO::PARAM_INT); | |
$stmt->bindParam(':cantidad', $cantidad, PDO::PARAM_INT); | |
if($stmt->execute()){ | |
return $stmt->fetchAll(PDO::FETCH_ASSOC); | |
//print_r($queryResult); | |
} | |
} catch (PDOException $ex){ | |
echo "<strong>Error de ejecución: </strong>" . $ex->getMessage() . "<br>"; | |
die(); | |
} | |
} | |
function obtenerTotalEstados($conexion){ | |
try{ | |
$query = 'SELECT count(*) as total FROM estados'; | |
$stmt = $conexion->prepare($query); | |
if($stmt->execute()){ | |
$total = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
return intval($total[0]['total']); | |
//print_r($queryResult); | |
} | |
}catch (PDOException $ex){ | |
echo "<strong>Error de ejecución: </strong>" . $ex->getMessage() . "<br>"; | |
die(); | |
} | |
} | |
function calcularPaginas($totalRegistros, $cantidadPorPagina = 10){ | |
return $totalRegistros / $cantidadPorPagina; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#Totas las funciones necesarias | |
require('functions.php'); | |
#Recepción de variables | |
$mi_inicio = intval($_GET['inicio']); | |
$mi_cantidad = intval($_GET['cantidad']); | |
#conexion a la Base de datos | |
$conexion_db = conexionBase(); | |
#Mis registros en la base de datos | |
$estados = consultarEstados($conexion_db, $mi_inicio, $mi_cantidad); | |
#Cantidad de registros en la base de datos | |
$total = obtenerTotalEstados($conexion_db); | |
#Cantidad de paginas a mostrar | |
$paginas = calcularPaginas($total, $mi_cantidad); | |
#pintado de datos ---- | |
$html_estados = ""; | |
foreach($estados as $estado){ | |
$html_estados .= "<tr> | |
<td>" . $estado['idestados'] . "</td> | |
<td>" . $estado['estado'] . "</td> | |
</tr>"; | |
} | |
#pintado de páginas ---- | |
$html_paginador = ""; | |
$pagina_inicio = 0; | |
for($i = 0; $i < $paginas; $i++){ | |
$pagina_siguiente = $pagina_inicio + $mi_cantidad; | |
if($i != 0){ | |
$html_paginador .= ' | '; | |
} | |
$html_paginador .= '<a href="index.php?inicio=' . $pagina_inicio . '&cantidad='. $mi_cantidad.'">'; | |
$html_paginador .= $i; | |
$html_paginador .= '</a>'; | |
$pagina_inicio = $pagina_siguiente; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h2>Estados de México</h2> | |
<table> | |
<tr> | |
<th> | |
- - | |
</th> | |
<th> | |
Estado | |
</th> | |
</tr> | |
<?php echo $html_estados ?> | |
</table> | |
<div> | |
<?php echo $html_paginador ?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment