Advertisement
Guest User

Untitled

a guest
May 6th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2. /*
  3. * @author Jota Vicente <jotavrj at gmail dot com>
  4. *
  5. */
  6. class Connection extends PDO { #classe de conexão com o BD
  7.     private $dsn = 'mysql:dbname=testes;host=127.0.0.1';
  8.     private $user = 'root';
  9.     private $password = '';
  10.     public $handle = null;
  11.  
  12.     function __construct( ) {
  13.         try {
  14.             if ( $this->handle === null ) {
  15.                 $dbh = parent::__construct( $this->dsn , $this->user , $this->password );
  16.                 $this->handle = $dbh;
  17.                 return $this->handle;
  18.             }
  19.         }
  20.         catch ( PDOException $e ) {
  21.             echo 'Connection failed: ' . $e->getMessage( );
  22.             return false;
  23.         }
  24.     }
  25.  
  26.     function __destruct( ) {
  27.         $this->handle = NULL;
  28.     }
  29. }
  30.  
  31. class TextPag
  32.     {
  33.     private $text = null;
  34.  
  35.     function __construct( $id )
  36.     {
  37.         try #técnica do grande caça moscas, meakMEKEMkMEKAMEkAME
  38.        {
  39.             $conn = new Connection(); #conecta
  40.            $sql = "SELECT text, author, id FROM texts WHERE id = :id"; /* faz a query, aqui voce muda onde quiser de acordo com necessidades */
  41.             $dbh = $conn->prepare($sql);
  42.             $dbh->bindParam(":id", $id, PDO::PARAM_INT); #verificacao de segurança
  43.            $dbh->execute();
  44.             $text = $dbh->fetchAll();
  45.             $textp = $text['text']; #separa só a parte do texto
  46.            $otherinfo = array($text['author'], $text['id']); #faz um array com as outras informações que solicitei na query
  47.            $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*/
  48.             $this->text = array($otherinfo, $textp); # faz um array bidimensional com todas as informações separas
  49.            return $this->text;
  50.         }
  51.         catch ( Exception $e )
  52.         {
  53.             echo "Houve um erro: ".$e->getMessage();
  54.         }
  55.     }
  56.    
  57.     function showPagination( $pg )
  58.     {
  59.         $pg = (int) $pg; #faz virar int
  60.        $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*/
  61.         echo "Escrito por: ".$this->text[0][0]."<br /><br />"; /* dou echo simples nas informações, mude se quiser*/
  62.         echo $this->text[1][$pg]; /* aqui tá a parte, dá echo na parte do texto que quiser*/
  63.     }
  64.    
  65.     function __set($attr, $value) {
  66.    
  67.         $this->$attr = $value;
  68.    
  69.     }
  70.    
  71.     function __toString() {
  72.    
  73.         for($i = 1, $count = sizeof($this->textp); $i <= $count; $i++) {
  74.        
  75.             $this->links .= '<a class="' . $this->classe . '" href="?id=' . $this->id . '&pg=' . $i . '">' . $i . '</a>';
  76.        
  77.         }
  78.        
  79.         return $this->links;
  80.  
  81.     }
  82. }
  83.  
  84. #exemplo de uso
  85.  
  86. $pag = new TextPag(15);
  87. $pag->showPagination(1);
  88.  
  89. // exibindo os links
  90. $pag->classe = 'link-pag';
  91. echo $pag;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement