Ti ho scritto un codice di esempio completo e funzionante, così riesci ad orientarti meglio.
Tieni presente che
- il nome del db è "new_schema"
- il nome della tabella è "tabella"
- La tabella ha due campi di nome: "id, campo"
- Devi riadeguare il codice per la mysql_connect
Codice PHP:
<?php
//LA CLASSE PER IL PAGING
class Paging {
private $totPag; //Totale Pagine nella tabella
private $pagCorr; //Pagina corrente
private $totRighe; //Totale righe nella tabella
private $righePerPagina; //Numero di righe per pagina da ottenere
//Facilita l'accesso all'array dell'intervallo
const kIntervalloInizio='inizio';
const kIntervalloLunghezza='lunghezza';
// Il costruttore chiede quale sia la pagina corrente, il totale delle
// righe in tabella e quante righe per pagina interessano
public function __construct($pagCorr,$totRighe,$righePerPagina) {
$this->pagCorr=$pagCorr;
$this->totRighe=$totRighe;
$this->righePerPagina=$righePerPagina;
$this->totPag=$this->contaPagine($totRighe, $righePerPagina);
}
// Consente di modificare la pagina corrente con un'altra dopo
// l'esecuzione del construct
public function vaiAPagina($pagina){
if($pagina==self::kUltimaPagina)
$this->pagCorr=$this->totPag;
elseif(is_numeric($pagina) and $pagina>1 and $pagina<$this->totPag)
$this->pagCorr=$pagina;
else
$this->pagCorr=1;
}
//Indica in quante pagine viene divisa la tabella
public function contaPagine($totRighe, $righePerPagina) {
return ceil($totRighe / $righePerPagina);
}
//Restituisce un array indicante la posizione del primo record e quante
//righe occorre scorrere
public function dammiIntervallo() {
return array(
self::kIntervalloInizio => ($this->pagCorr-1) * $this->righePerPagina,
self::kIntervalloLunghezza => $this->righePerPagina
);
}
//Ritorna il numero per la prossima pagina
public function prossimaPagina(){
if($this->totPag==0)
return 1;
return ($this->pagCorr>=$this->totPag)?$this->totPag:$this->pagCorr+1;
}
//Ritorna il numero per la pagina precedente
public function precedentePagina(){
return ($this->pagCorr<=1)?1:$this->pagCorr-1;
}
}
//PREPARA L'OGGETTO PAGING E LO RITORNA
function CostruisciOggettoPaging($sql) {
if (!isset($_GET['p']))
$pagCorr = 1;
else
$pagCorr=$_GET['p'];
$res = mysql_query($sql) or die('Err3');
$totRighe = mysql_num_rows($res);
mysql_free_result($res);
$righePerPagina = 3;
return new Paging($pagCorr, $totRighe, $righePerPagina);
}
//REALIZZA LA TABELLA E LA RITORNA SOTTO FORMA DI STRINGA
function CostruisciTabella($p,$sql) {
$inter=$p->dammiIntervallo();
$res = mysql_query("$sql limit {$inter[Paging::kIntervalloInizio]},{$inter[Paging::kIntervalloLunghezza]}") or die('Err4');
ob_start();
//Definizione della tabella
echo <<<END
<table border="1" cellpadding="10px">
<caption>Cattura record dal {$inter[Paging::kIntervalloInizio]} avanzando di {$inter[Paging::kIntervalloLunghezza]}</caption>
<tr>
<th>ID</th>
<th>Campo</th>
</tr>
END;
while ($row = mysql_fetch_assoc($res)) {
echo <<<END
<tr>
<td>{$row['id']}</td>
<td>{$row['campo']}</td>
</tr>
END;
}
//Link di navigazione
echo <<<END
<th colspan="2">
[url="{$_SERVER['PHP_SELF']}?p={$p->precedentePagina()}"]Indietro[/url] |
[url="{$_SERVER['PHP_SELF']}?p={$p->prossimaPagina()}"]Avanti[/url]
</th>
</table>
END;
mysql_free_result($res);
return ob_get_clean();
}
// CORPO PRINCIPALE DELLO SCRIPT
$conn=mysql_connect("localhost", "user", "password") or die('Err1');
mysql_select_db('new_schema',$conn) or die('Err2');
//Query magari da arricchire con una bella clausola where
$sql="select * from tabella";
$p = CostruisciOggettoPaging($sql);
$tabella_dati = CostruisciTabella($p,$sql);
mysql_close($conn);
//SEGUE LA VISUALIZZAZIONE DEI DATI IN PAGINA HTML
?>
<html>
<head>
<title>Esempio paginazione</title>
</head>
<body>
<?php echo $tabella_dati;?>
</body>
</html>