Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * @author Jota Vicente <jotavrj at gmail dot com>
- *
- */
- class Connection extends PDO { #classe de conexão com o BD
- private $dsn = 'mysql:dbname=testes;host=127.0.0.1';
- private $user = 'root';
- private $password = '';
- public $handle = null;
- function __construct( ) {
- try {
- if ( $this->handle === null ) {
- $dbh = parent::__construct( $this->dsn , $this->user , $this->password );
- $this->handle = $dbh;
- return $this->handle;
- }
- }
- catch ( PDOException $e ) {
- echo 'Connection failed: ' . $e->getMessage( );
- return false;
- }
- }
- function __destruct( ) {
- $this->handle = NULL;
- }
- }
- class TextPag
- {
- private $text = null;
- private $pag = null;
- public $classe = 'text';
- function __construct( $id = $_GET['id'] )
- {
- try #técnica do grande caça moscas, meakMEKEMkMEKAMEkAME
- {
- $conn = new Connection(); #conecta
- $sql = "SELECT text, author, id FROM texts WHERE id = :id"; /* faz a query, aqui voce muda onde quiser de acordo com necessidades */
- $dbh = $conn->prepare($sql);
- $dbh->bindParam(":id", $id, PDO::PARAM_INT); #verificacao de segurança
- $dbh->execute();
- $text = $dbh->fetchAll();
- $textp = $text['text']; #separa só a parte do texto
- $otherinfo = array($text['author'], $text['id']); #faz um array com as outras informações que solicitei na query
- $textp = explode("[[pgbrk]]", $textp); /* aqui tá a parte da lógica, onde no texto você, quando vê que tem que mudar de página, escreve [[pgbrk]]. você pode mudar de acordo com sua preferência, colocando tags de html, por exemplo, mas acho melhor assim*/
- $this->text = array($otherinfo, $textp); # faz um array bidimensional com todas as informações separas
- return $this->text;
- }
- catch ( Exception $e )
- {
- echo "Houve um erro: ".$e->getMessage();
- }
- }
- function showPagination( $pg = $_GET['page'] )
- {
- $pg = (int) $pg; #faz virar int
- $pg -= 1; /* como é um array, a página 1 corresponde à $text[0], a página 2 corresponde à $text[1], e assim por diante, logo, o número dá página é sempre 1 a mais que o índice do array. aqui eu tiro essa diferença*/
- echo "Escrito por: ".$this->text[0][0]."<br /><br />"; /* dou echo simples nas informações, mude se quiser*/
- $this->pag = $pg;
- echo $this->text[1][$pg]; /* aqui tá a parte, dá echo na parte do texto que quiser*/
- }
- function generateLinks( )
- {
- $link = '';
- $textp = count($this->text[1]);
- for( $i = 1; $i <= $textp; $i++ ) #faz o loop das páginas
- {
- if ( $i == $pg )
- {
- $link .= '[ <strong>'.$pg.'</strong> ]'; #se o loop estiver na página atual, não cria link
- }
- else
- {
- $link .= '[ <a class="'.$this->classe.'" href="?id='.$this->text[0][1].'&page='.$i.'">'.$i.'</a> ]';
- }
- }
- echo $link;
- }
- }
- #exemplo de uso
- # estou na página artigo.php?id=15&page=1
- $pag = new TextPag();
- $pag->showPagination();
- $pag->generateLinks(); /*poderia estar junto do showPagination, mas pra dar liberdade para encaixar onde quiser no layout, separei em outro método */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement