Hoje vamos aprender a fazer paginação em php ex 123 etc…. pode ser usado no seu site ou para outras coisas lembrando tem varias outras formas de fazer paginação em php este é apenas um mero exemplo.
--
-- Banco de Dados: `paginacao`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `paginacao`
--
CREATE TABLE IF NOT EXISTS `paginacao` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`telefone` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
--
-- Extraindo dados da tabela `paginacao`
--
INSERT INTO `paginacao` (`id`, `nome`, `email`, `telefone`) VALUES
(1, 'ozorio', 'ozoriotsn@hotmail.com', '51-91295705'),
(2, 'bruna surfistinha', 'bruna@gmail.com', '51-96857854'),
(3, 'carol safada', 'carol@hotmail.com', '51-69582659'),
(4, 'michele gostosinha', 'mi@hotmail.com', '51-9128598787'),
(5, 'samara maravilha', 'mara@gmail.com', '51-912856958'),
(6, 'luiza quete', 'lza@gmail.com', '51-96857458'),
(7, 'marcia brum', 'mar@bol.com.br', '51-91295658'),
(8, 'karen vai', 'kan@gmail.com', '51-54545645454');
Faça a pagina com o nome Connect.php
<?php
class Conexao {
public $host = "";
public $user = "";
public $pass = "";
public $database = "";
public $connect = "";
public $select = "";
function __construct() {
$this->host="localhost";
$this->user="root";
$this->pass="123";
$this->database="paginacao";
$this->connect=mysql_connect($this->host,$this->user,$this->pass) or die(mysql_error());
$this->select=mysql_select_db($this->database,$this->connect) or die(mysql_error());
}
}
$conexao = new Conexao();
?>
Faça a pagina que sera o Inicio do sistema e sera a pagina dos Resultados com o nome de index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Paginação em Php </title>
</head>
<body>
<?php
include_once 'connect.php';
class Paginacao{
//////////CONTEUDO/////////////
public $pg = "";
public $qtd = "";
public $ini = "";
public $query1 = "";
public $row = "";
public $nome = "";
//////////LINKS///////////////
public $query2 = "";
public $total_reg="";
public $pgs="";
public $lk="";
public $numpg="";
function Conteudo() {
$this->pg= $_GET['pg'];//paginas
if(isset($this->pg)){// verificamos se a variavel pg existe
$this->pg = $this->pg;
}else{
$this->pg=1;
}
$this->qtd=3;//quantidade de dados em cada pagina
$this->ini=($this->pg*$this->qtd) - $this->qtd;
//inicio | pagina vezes quantidade menos(-) quantidade
// se pagina for = 2 e quantidade = 3 resultado = 6 - 3=quantidade resultado final= 3
$this->query1 = mysql_query("SELECT * FROM paginacao ORDER BY nome LIMIT $this->ini,$this->qtd");
while($this->row = mysql_fetch_object($this->query1)){
$this->nome = $this->row->nome;
$this->email = $this->row->email;
$this->telefone = $this->row->telefone;
echo "<b>Nome:</b> $this->nome<br>";
echo "<b>Email:</b> $this->email<br>";
echo "<b>Telefone:</b> $this->telefone<br>";
echo "<hr>";
}
}
function Links() {
$this->query2 = mysql_query("SELECT * FROM paginacao");
$this->total_reg = mysql_num_rows($this->query2);
$this->pgs = ceil($this->total_reg/$this->qtd); //ceil (arredonda numeros 10.9=11) total de paginas| total de registros dividido por total de quantidades
$this->lk = 1;// link
echo "<a href='index.php?pg=1'><< Primeira </a> ";
// para var:numpg =pagina menos link; var:numpg menor ou = a pagina -1; var:numpg incrementa + 1
for($this->numpg = $this->pg - $this->lk; $this->numpg <= $this->pg - 1; $this->numpg++){
if($this->numpg<=0){
}else{
echo " <a href='index.php?pg=".$this->numpg."'>".$this->numpg."</a> ";
}
}
echo "<a href=#>$this->pg</a>";// link morto
for($this->numpg = $this->pg+1; $this->numpg <= $this->pg+$this->lk; $this->numpg++){
if($this->numpg > $this->pgs){
}else{
echo " <a href='index.php?pg=".$this->numpg."'>".$this->numpg."</a> ";
}
}
echo " <a href='index.php?pg=".$this->pgs."'>Ultima >> </a> ";
}
}
//instaciamos o objeto
$paginacao = new Paginacao();
$paginacao->Conteudo();
$paginacao->Links();
?>
</body>
</html>
0 Comentários:
Observação: somente um membro deste blog pode postar um comentário.