this line:
$sql.= " LIMIT $pagesize, $offset ";
only work well when i do let $offset, out of the code, so the script shows the number of pages, but do not make a link to the others pages, if $offset stay in this line it give me the folowing erros:
pg_query() query failed: ERROR: LIMIT #,# syntax not supported. Use separate LIMIT and OFFSET clauses. in /var/www/html/paginar.php on line 38
pg_num_fields(): supplied argument is not a valid PostgreSQL result resource in /var/www/html/paginar.php on line 52
pg_fetch_row(): supplied argument is not a valid PostgreSQL result resource in /var/www/html/paginar.php on line 54
Why this happens?
how to make it work?
Below is the complete code:
<?php
global $limit, $sql, $url, $pagesize, $pagecount, $absolutepage, $recordcount;
// Configuration...
$db = pg_connect("dbname=db user=user");
$sql= "SELECT FROM table";
$pagesize = 20 ;
$url = $_SERVER['PHP_SELF'];
if (!isset($absolutepage)) { // Set offset and absolutepage if not set
$absolutepage = 1;
$offset = 0;
} else {
$offset = (( $absolutepage - 1 ) $pagesize );
}
if (!isset($total_rows)) { // Get total_rows at startup
$result = pg_query($sql);
$recordcount = ( pg_numrows($result) ) -1;
$pagecount = intval($recordcount / $pagesize);
if ($recordcount % $pagesize ) $pagecount++;
}
if ($recordcount > 0) {
$sql.= " LIMIT $pagesize, $offset "; //Get record set = pagesize every time here is the trouble!
$result = pg_query($sql);
if ($absolutepage == 1) {
$pag1 = $absolutepage;
} else {
$pag1 = (($absolutepage - 1) * $pagesize) + 1;
}
$pag2 = $pag1 + ($pagesize - 1);
if ($pag2 > $recordcount)
$pag2 = $recordcount;
echo "<hr><font face='verdana, arial' size=2>";
echo "Exibindo registros $pag1 á $pag2 (of " . $recordcount.")<hr>";
PagNav($pagesize, $pagecount, $absolutepage, $recordcount, $sql, $url) ;
echo "<hr>";
$fields = pg_num_fields($result);
for ($i=0; $i < $pagesize; $i++) {
$data = pg_fetch_row($result);
for ($j=0; $j < $fields; $j++)
{
echo $data[$j] . " | ";
}
echo "<br>\n";
}
} else {
echo "Registros não encontrados.";
}
echo "</body></html>";
function PagNav($pagesize, $pagecount, $absolutepage, $recordcount, $sql, $url)
{
global $sql, $url, $pagesize, $pagecount, $absolutepage, $recordcount;
$pad="" ;
$maxpages = $pagecount ;
if ( ($absolutepage % 10) == 0) {
$counterstart = $absolutepage - 9 ;
} else {
$counterstart = $absolutepage - ($absolutepage % 10) + 1 ;
}
$counterend = $counterstart + 9 ;
echo "<font size=1><b>Página ".$absolutepage." de ".$pagecount.": </b>";
if ($counterend > $maxpages) $counterend = $maxpages ;
if ($counterstart != 1) {
$ref = "<a href='" . $url ;
$ref .= "?absolutepage=" . "1" ;
$ref .= "'>Primeiro</a> | " ;
echo $ref ;
$ref = "<a href='" . $url ;
$ref .= "?absolutepage=" . ($counterstart - 1) ;
$ref .= "'>10 Páginas anteriores</a> " ;
echo $ref ;
}
echo "<b>[ </b>" ;
for ($counter = $counterstart; $counter < $counterend +1;$counter++ ) {
if ($counter >= 10) $pad="" ;
if (! ($counter == $absolutepage)) {
$ref = "<a href='" . $url ;
$ref .= "?absolutepage=" . $counter ;
$ref .= "'>" . $pad . $counter . "</a>" ;
} else {
$ref = "<b>" . $pad . $counter . "</b>" ;
}
echo $ref ;
if (!($counter == $counterend)) print " " ;
}
echo "<b> ]</b>" ;
if ($counterend != $maxpages) {
$ref = " <a href='" . $url ;
$ref .= "?absolutepage=" . ($counterend + 1) ;
$ref .= "'>Próximas 10 Páginas</a>" ;
echo $ref ;
$ref = " | <a href='" . $url ;
$ref .= "?absolutepage=" . $maxpages ;
$ref .= "'>Ùltima</a>" ;
echo $ref ;
}
echo "</font>";
}
?>
Best regards