ho creato una classe che permette di dividere in pagine i record di una query... magicamente funziona!
Codice PHP:
<?php
class dividixpag
{
var $currentPag;
var $totPages;
var $totElements;
function dividixpag()
{
//
}
function dividiPag($query, $pag = 0, $xPag = 5)
{
global $db;
$this->currentPag = $pag;
$db->query($query);
$this->totElements = $db->num_rows;
if($this->currentPag == 0)
{
$this->totPages = -1;
$limit_da = 0;
$num = $this->totElements;
}
else
{
$this->totPages = ceil($this->totElements / $xPag);
$limit_da = ($this->currentPag*$xPag-($xPag-1))-1;
$limit_a = $this->currentPag*$xPag;
if($limit_a>$this->totElements)
$num = $this->totElements - $limit_da;
else
$num= $xPag;
}
return array($limit_da, $num);
}
function showPages()
{
$link="index2.php";
$qs=$_SERVER['QUERY_STRING'];
$qs=eregi_replace("^(&p=)[0-9]*$","&p=",$qs);
if (!ereg("^(p=)$", $qs))
$qs.="&p=";
$link.="?".$qs;
if($this->currentPag!=1)
{
$prec = $this->currentPag - 1;
echo "<a href=\"".$link."1\" title=\"Pag n. 1\">"."Prima"."</a>-\n";
echo "<a href=\"".$link.$prec."\" title=\"Pag n. ".$prec."\"><<<</a>-\n";
}
else
echo "Prima"." - <<< - ";
for($x=1; $x<=$this->totPages; $x++)
{
if($x==$this->currentPag)
echo $x;
else
echo " <a href=\"".$link.$x."\" title=\"Pag n. $x\">$x</a> \n";
echo " - ";
}
if($this->currentPag!=$this->totPages)
{
$succ = $this->currentPag + 1;
echo " <a href=\"".$link.$succ."\" title=\"Pag n. ".$succ."\">>>></a>-\n";
echo " <a href=\"".$link.$succ."\" title=\"Pag n. ".$this->totPages."\">"."Ultima"."</a>\n";
}
else
echo ">>> - "."Ultima";
}
}
?>
Nel programma istanzio la classe carrello
$carrello = new carrello();
e la classe per dividere
$dividi = new dividixpag();
nel metodo carrello::getCarrelli() ho messo
global $db; // Recupera la connessione
global $dividi; Recupera l'oggetto per dividere la query in pagine...
solo che mi da: Fatal error: Call to a member function on a non-object in /web/htdocs/www.milanogiovanni.net/home/lib/php/classes/carrelli/carrelli.php on line 67
perchè?