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;
- function __construct( $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 )
- {
- $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*/
- echo $this->text[1][$pg]; /* aqui tá a parte, dá echo na parte do texto que quiser*/
- }
- function __set($attr, $value) {
- $this->$attr = $value;
- }
- function __toString() {
- for($i = 1, $count = sizeof($this->textp); $i <= $count; $i++) {
- $this->links .= '<a class="' . $this->classe . '" href="?id=' . $this->id . '&pg=' . $i . '">' . $i . '</a>';
- }
- return $this->links;
- }
- }
- #exemplo de uso
- $pag = new TextPag(15);
- $pag->showPagination(1);
- // exibindo os links
- $pag->classe = 'link-pag';
- echo $pag;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement