[PHP] inserire paginazione su script php/mysql

Andrea Crocco

Utente Attivo
27 Apr 2016
77
0
6
salve vorrei aggiungere sulla mia pagina web uno script commenti.
il problema è che questo script non ha dei limiti sui commenti.
mi spiego meglio...
ho visto diversi script che dopo 10 commenti scritti dagli utenti, essi creavano delle pagine.
in ogni pagina c'erano appunto 10 commenti.
che stringa devo aggiungere sul mio script per fare ciò?
PHP:
<?php

class Comment
{
    private $data = array();
   
    public function __construct($row)
    {
        /*
        /    The constructor
        */
       
        $this->data = $row;
    }
   
    public function markup()
    {
        /*
        /    This method outputs the XHTML markup of the comment
        */
       
        // Setting up an alias, so we don't have to write $this->data every time:
        $d = &$this->data;
       
        $link_open = '';
        $link_close = '';
       
        if($d['url']){
           
            // If the person has entered a URL when adding a comment,
            // define opening and closing hyperlink tags
           
            $link_open = '<a href="'.$d['url'].'">';
            $link_close =  '</a>';
        }
       
        // Converting the time to a UNIX timestamp:
        $d['dt'] = strtotime($d['dt']);
       
        // Needed for the default gravatar image:
        $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
       
        return '
       
            <div class="comment">
                <div class="avatar">
                    '.$link_open.'
                    <img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;default='.urlencode($url).'" />
                    '.$link_close.'
                </div>
               
                <div class="name">'.$link_open.$d['name'].$link_close.'</div>
                <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
                <p>'.$d['body'].'</p>
            </div>
        ';
    }
   
    public static function validate(&$arr)
    {
        /*
        /    This method is used to validate the data sent via AJAX.
        /
        /    It return true/false depending on whether the data is valid, and populates
        /    the $arr array passed as a paremter (notice the ampersand above) with
        /    either the valid input data, or the error messages.
        */
       
        $errors = array();
        $data    = array();
       
        // Using the filter_input function introduced in PHP 5.2.0
       
        if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
        {
            $errors['email'] = 'Inserisci il tuo indirizzo Email.';
        }
       
        if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
        {
            // If the URL field was not populated with a valid URL,
            // act as if no URL was entered at all:
           
            $url = '';
        }
       
        // Using the filter with a custom callback function:
       
        if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['body'] = 'Inserisci un commento valido.';
        }
       
        if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['name'] = 'Inserisci il tuo nome.';
        }
       
        if(!empty($errors)){
           
            // If there are errors, copy the $errors array to $arr:
           
            $arr = $errors;
            return false;
        }
       
        // If the data is valid, sanitize all the data and copy it to $arr:
       
        foreach($data as $k=>$v){
            $arr[$k] = mysql_real_escape_string($v);
        }
       
        // Ensure that the email is lower case:
       
        $arr['email'] = strtolower(trim($arr['email']));
       
        return true;
       
    }

    private static function validate_text($str)
    {
        /*
        /    This method is used internally as a FILTER_CALLBACK
        */
       
        if(mb_strlen($str,'utf8')<1)
            return false;
       
        // Encode all html special characters (<, >, ", & .. etc) and convert
        // the new line characters to <br> tags:
       
        $str = nl2br(htmlspecialchars($str));
       
        // Remove the new line characters that are left
        $str = str_replace(array(chr(10),chr(13)),'',$str);
       
        return $str;
    }

}

?>
 

Andrea Crocco

Utente Attivo
27 Apr 2016
77
0
6
innanzitutto grazie per il link.
ho provato ad integrarlo ma...non riesce a disporre i vari commenti nelle varie pagine
ecco come ho fatto:
PHP:
<?php
//includiamo il file della classe
@require("paginazione.php");

//connettiamoci a MySQL e selezioniamo il database
class MySQL
{
  function MySQL()
  {
    $this->host_name = "localhost";
    $this->user_name = "andreacomments";
    $this->password = "inventore999";
    $this->data_name = "commenti";
    $this->link = @mysql_connect($this->host_name, $this->user_name, $this->password) or die (mysq_error());
    @mysql_select_db($this->data_name) or die (mysq_error());
  }
}

$data = new MySQL();
// istanziamo la classe per l'impaginazione
$p = new Paging;

// numero massimo di risultati per pagina
$max = 2;

// identifichiamo la pagina da cui iniziare la numerazione
$inizio = $p->paginaIniziale($max);

// contiamo i records nel database
$query_count = @mysql_query("SELECT * FROM comments") or die (mysql_error());
$count = @mysql_num_rows($query_count) or die (mysql_error());

// troviamo il numero delle pagine che dovrà essere contato
$pagine = $p->contaPagine($count, $max);

// limitiamo la SELECT al numero di risultati per pagina
$query = @mysql_query("SELECT * FROM comments LIMIT ".$inizio.",".$max) or die (mysql_error());

//mostriamo le pagine
$lista = $p->listaPagine($_GET['p'], $pagine);
echo $lista . "<br>";

//mostriamo il navigatore Precedente/Successiva
$navigatore = $p->precedenteSuccessiva($_GET['p'], $pagine);
echo $navigatore;
?>
<?php

class Comment
{
    private $data = array();
   
    public function __construct($row)
    {
        /*
        /    The constructor
        */
       
        $this->data = $row;
    }
   
    public function markup()
    {
        /*
        /    This method outputs the XHTML markup of the comment
        */
       
        // Setting up an alias, so we don't have to write $this->data every time:
        $d = &$this->data;
       
        $link_open = '';
        $link_close = '';
       
        if($d['url']){
           
            // If the person has entered a URL when adding a comment,
            // define opening and closing hyperlink tags
           
            $link_open = '<a href="'.$d['url'].'">';
            $link_close =  '</a>';
        }
       
        // Converting the time to a UNIX timestamp:
        $d['dt'] = strtotime($d['dt']);
       
        // Needed for the default gravatar image:
        $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
       
        return '
       
            <div class="comment">
                <div class="avatar">
                    '.$link_open.'
                    <img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;default='.urlencode($url).'" />
                    '.$link_close.'
                </div>
               
                <div class="name">'.$link_open.$d['name'].$link_close.'</div>
                <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
                <p>'.$d['body'].'</p>
            </div>
        ';
    }
   
    public static function validate(&$arr)
    {
        /*
        /    This method is used to validate the data sent via AJAX.
        /
        /    It return true/false depending on whether the data is valid, and populates
        /    the $arr array passed as a paremter (notice the ampersand above) with
        /    either the valid input data, or the error messages.
        */
       
        $errors = array();
        $data    = array();
       
        // Using the filter_input function introduced in PHP 5.2.0
       
        if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
        {
            $errors['email'] = 'Inserisci il tuo indirizzo Email.';
        }
       
        if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
        {
            // If the URL field was not populated with a valid URL,
            // act as if no URL was entered at all:
           
            $url = '';
        }
       
        // Using the filter with a custom callback function:
       
        if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['body'] = 'Inserisci un commento valido.';
        }
       
        if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['name'] = 'Inserisci il tuo nome.';
        }
       
        if(!empty($errors)){
           
            // If there are errors, copy the $errors array to $arr:
           
            $arr = $errors;
            return false;
        }
       
        // If the data is valid, sanitize all the data and copy it to $arr:
       
        foreach($data as $k=>$v){
            $arr[$k] = mysql_real_escape_string($v);
        }
       
        // Ensure that the email is lower case:
       
        $arr['email'] = strtolower(trim($arr['email']));
       
        return true;
       
    }

    private static function validate_text($str)
    {
        /*
        /    This method is used internally as a FILTER_CALLBACK
        */
       
        if(mb_strlen($str,'utf8')<1)
            return false;
       
        // Encode all html special characters (<, >, ", & .. etc) and convert
        // the new line characters to <br> tags:
       
        $str = nl2br(htmlspecialchars($str));
       
        // Remove the new line characters that are left
        $str = str_replace(array(chr(10),chr(13)),'',$str);
       
        return $str;
    }

}

?>
 

cris8380

Moderatore
Membro dello Staff
MOD
3 Giu 2016
261
14
18
40
Ciao Andrea ben ritrovato
Mi dici come hai predisposto il db o magari allega uno screanshoot!
 

Andrea Crocco

Utente Attivo
27 Apr 2016
77
0
6
ciao cris! è un piacere vederti:)...
allora ecco gli screen:
Immag4ine.png
Immagin2e.png
 
Discussioni simili
Autore Titolo Forum Risposte Data
Couting95 inserire dati da un file di testo in una tabella in php PHP 1
A inserire variabile php colore in div html PHP 2
D [Javascript] inserire uno script in un file php Javascript 6
M [PHP] Come inserire codice html in un ciclo while PHP 2
P [PHP] Inserire stringhe in input(text),memorizzarle e stamparle in file successivo PHP 0
Shyson [PHP] Inserire testo nel codice PHP 2
Alex_70 Inserire photo in php PHP 0
spider81man [PHP] Inserire file .pdf in db PHP 6
A [PHP] Ciclare array multidimensionale e inserire valori in DB PHP 2
M [PHP] Inserire array nel db PHP 6
G [PHP] inserire risultato di una query in una tabella PHP 3
M [wordpress-galleria immagini]Inserire classe php in html PHP 0
C Inserire dati tabella leggendo parte di altra tabella con php PHP 13
crealatualista [PHP] Inserire nomi nel database PHP 1
L [PHP] Inserire google recaptcha in uno form contatti PHP 1
T4MAR4 [PHP] Inserire piu campi di ricerca PHP 2
D Come inserire opzioni menù prelevandole dal database con PHP ? PHP 12
M PHP/SQL Inserire più valori in una colonna di tipo integer - Checkbox - PHP 3
G Google recaptcha in verify.php - dove inserire il codice PHP 1
G [Javascript] jQuery PHP MySql - inserire variabile nel DataBase Javascript 8
giancadeejay inserire condizione php PHP 6
N [PHP] inserire campi tabella in file word PHP 0
9 inserire contenuto in un file pdf aperto sul browser tramite script php PHP 0
E [PHP] inserire HTML in una stringa PHP 16
S Come inserire file in database Mysql senza scrivere codice PHP? PHP 0
L Inserire codice php dentro al javascript PHP 4
L [PHP/HTML] Inserire metodo di pagamento PHP 4
M inserire articolo nel blog php PHP 7
D inserire foto in database php PHP 2
D Inserire script php in un sito PHP 1
neo996sps Inserire grafico google analytics in pagina PHP Google Analytics 0
T Inserire automaticamente Categorie in hikashop da php mysql PHP 0
T [PHP] inserire un file pdf da poter far scaricare... PHP 22
M inserire codice php in articolo joomla Joomla 2
A PHP calendario, come inserire i giorni precedenti? PHP 1
D php e maschera per inserire dati in mysql PHP 4
A Inserire codice html gooole maps in una variabile php PHP 1
P [PHP] Inserire email in un database PHP 2
A inserire nel codice php una stringa xml PHP 0
A inserire messaggio php al centro pagina PHP 4
N come inserire un supporto php per il web ? HTML e CSS 1
Matt89 [php gd library] inserire un'immagine in un canvas PHP 3
M Aiuto come inserire dati tramite php? PHP 1
L Inserire uno file TXT....in PHP PHP 1
F Cerco Hosting con VECCHIE versioni di php Hosting 0
Cosina Captcha php PHP 1
S passare un valore da un form a un file .php con metodo post PHP 4
N php msyql PHP 6
N php problemi a visualizzare video PHP 3
A menu a tendina php PHP 1

Discussioni simili