Paging results
HI there,
Thanks to everyone who helped with my keyword problem - I think I thanked
them all individually but I thought I should mention it here too.
Now, a new question:
Does anyone know if there's a PHP class anywhere out there for paging
results from a PostgreSQL query, similar to Paginator or ezResults which do
that for MySQL? Or do I have to do the code for that from scratch?
Alternatively, would it be difficult to adapt one of those to working with
PostgreSQL instead of MySQL?
Lynna
--
Resource Centre Database Coordinator
Gallery 44
www.gallery44.org
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org
Re: Paging results
Paging with PostgreSQL is super easy!
select * from table where (x=y) offset 0 limit 30;
Gives you the first 30 matches, then do
select * from table where (x=y) offset 30 limit 30;
This will give the next 30, super easy!
Here's a sample of how I use it in a script
// Collect offset
$offset = isset($_GET['offset'])?$_GET['offset']:0;
// Now the links for Prev/Next
if ($offset >= 30) echo "|<a
href='/contents.php?".$qs."offset=".($offset-30)."'>Back 30</a>";
echo "|<a href='/contents.php?".$qs."offset=".($offset+30)."'>Next 30</a>";
// Query
$rs = pg_exec($db,"select id,name from stuff order by name offset $offset
limit 30;");
/B
----- Original Message -----
From: "Lynna Landstreet" <lynna [at] gallery44.org>
To: <pgsql-php [at] postgresql.org>
Sent: Friday, August 08, 2003 13:30
Subject: [PHP] Paging results
> HI there,
>
> Thanks to everyone who helped with my keyword problem - I think I thanked
> them all individually but I thought I should mention it here too.
>
> Now, a new question:
>
> Does anyone know if there's a PHP class anywhere out there for paging
> results from a PostgreSQL query, similar to Paginator or ezResults which
do
> that for MySQL? Or do I have to do the code for that from scratch?
>
> Alternatively, would it be difficult to adapt one of those to working with
> PostgreSQL instead of MySQL?
>
>
> Lynna
> --
> Resource Centre Database Coordinator
> Gallery 44
> www.gallery44.org
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
Re: Paging results
I have this script (please adapt the section where the
results are showed)
<?php
// Request for parameters.
$offset =3D $_REQUEST['offset'];
$pgnum =3D $_REQUEST['pgnum'];
// Make that they are integer - security.
settype($offset, 'integer');
settype($pgnum, 'integer');
// Open database, i.e PostgreSQL
$connuserpostgres");
if (!$conn) {
echo "An error occured.\n";
exit;
}
// Initialize variables.
$limit=3D20; // rows to return
$numresults=3Dpg_query("select * from table"); //
PostgreSQL
$numrows=3Dpg_num_rows($numresults);
// next determine if offset has been passed to
script, if not use 0
if (empty($offset)) {
$offset=3D0;
$pgnum=3D1;
}
// get results
$result=3Dpg_query("select autor, titulo from table
limit $limit offset $offset"); // PostgreSQL
// now you can display the results returned
//echo "OffSet ".$offset." Page# ".$pgnum."<br><br>";
echo "<table><tr>";
echo "<td><b>Autor</b></td>";
echo "<td><b>Titulo</b></td>";
echo "</tr>";
while ($row =3D pg_fetch_array($result, $i)) {
echo "<tr><td align=3D\"left\">";
//$arr =3D pg_fetch_array($result,($i));
for ($j=3D0; $j < count($row); $j++) {
/*
echo "<tr><td align=3D\"left\" colspan=3D\"2\">";
echo "$row[$j] ";
echo "</td></tr>";
*/
echo "$row[$j]";
//echo " $j['autor']";
=09
=09
}
echo "</td></tr>";
//echo "<BR>";
}
echo "</table>";
// calculate number of pages needing links
$pages=3Dintval($numrows/$limit);
// $pages now contains int of pages needed unless
there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
echo "<font size=3D1>";
// next we need to do the links to other results
if (pages!=3D1)
{
if ($pgnum=3D=3D1) {
print "<a
href=3D\"$PHP_SELF?offset=3D0&pgnum=3D1\">PREV</a>
\n";
}
else
{
$prevoffset=3D$offset-20;
$cpgnum =3D intval($prevoffset/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$prevoffset&pgnum=3D$cpgnum\">PR EV</a>
\n";
}
}
for ($i=3D1;$i<=3D$pages;$i++) { // loop thru
$newoffset=3D$limit*($i-1);
$cpgnum =3D $i;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">$cp gnum</a>
\n";
}
// check to see if last page
if ($pages!=3D1)
{
if ($pgnum<$pages) {
$newoffset=3D$offset+$limit;
$cpgnum =3D intval(($offset+$limit)/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">NEX T</a><p>\n";
}
else
{
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$pages\">NEXT</a><p>\n";
}
}
// Close database.
pg_close($conn);
?>
--- David Busby <busby [at] pnts.com> escreveu: > Paging
with PostgreSQL is super easy!
> select * from table where (x=3Dy) offset 0 limit 30;
> Gives you the first 30 matches, then do
> select * from table where (x=3Dy) offset 30 limit
> 30;
> This will give the next 30, super easy!
>
> Here's a sample of how I use it in a script
>
> // Collect offset
> $offset =3D isset($_GET['offset'])?$_GET['offset']:0;
> // Now the links for Prev/Next
> if ($offset >=3D 30) echo "|<a
>
href=3D'/contents.php?".$qs."offset=3D".($offset-30)."'>Back
> 30</a>";
> echo "|<a
>
href=3D'/contents.php?".$qs."offset=3D".($offset+30)."'>Next
> 30</a>";
> // Query
> $rs =3D pg_exec($db,"select id,name from stuff order
> by name offset $offset
> limit 30;");
>
> /B
>
>
> ----- Original Message -----
> From: "Lynna Landstreet" <lynna [at] gallery44.org>
> To: <pgsql-php [at] postgresql.org>
> Sent: Friday, August 08, 2003 13:30
> Subject: [PHP] Paging results
>
>
> > HI there,
> >
> > Thanks to everyone who helped with my keyword
> problem - I think I thanked
> > them all individually but I thought I should
> mention it here too.
> >
> > Now, a new question:
> >
> > Does anyone know if there's a PHP class anywhere
> out there for paging
> > results from a PostgreSQL query, similar to
> Paginator or ezResults which
> do
> > that for MySQL? Or do I have to do the code for
> that from scratch?
> >
> > Alternatively, would it be difficult to adapt one
> of those to working with
> > PostgreSQL instead of MySQL?
> >
> >
> > Lynna
> > --
> > Resource Centre Database Coordinator
> > Gallery 44
> > www.gallery44.org
> >
> >
> > ---------------------------(end of
> broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to
> majordomo [at] postgresql.org
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 8: explain analyze is your friend
=3D=3D=3D=3D=3D
=C2ngelo Marcos Rigo
AMR Inform=E1tica
(51) 3348 0870
Rua Pe. Alois Kades 400/210
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo [at] yahoo.com.br
____________________________________________________________ ___________
Conhe=E7a o novo Cad=EA? - Mais r=E1pido, mais f=E1cil e mais preciso.
Toda a web, 42 milh=F5es de p=E1ginas brasileiras e nova busca por imagen=
s!
http://www.cade.com.br
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo [at] postgresql.org so that your
message can get through to the mailing list cleanly
Re: Paging results
I have this script (please adapt the section where the
results are showed)
<?php
// Request for parameters.
$offset =3D $_REQUEST['offset'];
$pgnum =3D $_REQUEST['pgnum'];
// Make that they are integer - security.
settype($offset, 'integer');
settype($pgnum, 'integer');
// Open database, i.e PostgreSQL
$connuserpostgres");
if (!$conn) {
echo "An error occured.\n";
exit;
}
// Initialize variables.
$limit=3D20; // rows to return
$numresults=3Dpg_query("select * from table"); //
PostgreSQL
$numrows=3Dpg_num_rows($numresults);
// next determine if offset has been passed to
script, if not use 0
if (empty($offset)) {
$offset=3D0;
$pgnum=3D1;
}
// get results
$result=3Dpg_query("select autor, titulo from table
limit $limit offset $offset"); // PostgreSQL
// now you can display the results returned
//echo "OffSet ".$offset." Page# ".$pgnum."<br><br>";
echo "<table><tr>";
echo "<td><b>Autor</b></td>";
echo "<td><b>Titulo</b></td>";
echo "</tr>";
while ($row =3D pg_fetch_array($result, $i)) {
echo "<tr><td align=3D\"left\">";
//$arr =3D pg_fetch_array($result,($i));
for ($j=3D0; $j < count($row); $j++) {
/*
echo "<tr><td align=3D\"left\" colspan=3D\"2\">";
echo "$row[$j] ";
echo "</td></tr>";
*/
echo "$row[$j]";
//echo " $j['autor']";
=09
=09
}
echo "</td></tr>";
//echo "<BR>";
}
echo "</table>";
// calculate number of pages needing links
$pages=3Dintval($numrows/$limit);
// $pages now contains int of pages needed unless
there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
echo "<font size=3D1>";
// next we need to do the links to other results
if (pages!=3D1)
{
if ($pgnum=3D=3D1) {
print "<a
href=3D\"$PHP_SELF?offset=3D0&pgnum=3D1\">PREV</a>
\n";
}
else
{
$prevoffset=3D$offset-20;
$cpgnum =3D intval($prevoffset/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$prevoffset&pgnum=3D$cpgnum\">PR EV</a>
\n";
}
}
for ($i=3D1;$i<=3D$pages;$i++) { // loop thru
$newoffset=3D$limit*($i-1);
$cpgnum =3D $i;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">$cp gnum</a>
\n";
}
// check to see if last page
if ($pages!=3D1)
{
if ($pgnum<$pages) {
$newoffset=3D$offset+$limit;
$cpgnum =3D intval(($offset+$limit)/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">NEX T</a><p>\n";
}
else
{
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$pages\">NEXT</a><p>\n";
}
}
// Close database.
pg_close($conn);
?>
--- David Busby <busby [at] pnts.com> escreveu: > Paging
with PostgreSQL is super easy!
> select * from table where (x=3Dy) offset 0 limit 30;
> Gives you the first 30 matches, then do
> select * from table where (x=3Dy) offset 30 limit
> 30;
> This will give the next 30, super easy!
>
> Here's a sample of how I use it in a script
>
> // Collect offset
> $offset =3D isset($_GET['offset'])?$_GET['offset']:0;
> // Now the links for Prev/Next
> if ($offset >=3D 30) echo "|<a
>
href=3D'/contents.php?".$qs."offset=3D".($offset-30)."'>Back
> 30</a>";
> echo "|<a
>
href=3D'/contents.php?".$qs."offset=3D".($offset+30)."'>Next
> 30</a>";
> // Query
> $rs =3D pg_exec($db,"select id,name from stuff order
> by name offset $offset
> limit 30;");
>
> /B
>
>
> ----- Original Message -----
> From: "Lynna Landstreet" <lynna [at] gallery44.org>
> To: <pgsql-php [at] postgresql.org>
> Sent: Friday, August 08, 2003 13:30
> Subject: [PHP] Paging results
>
>
> > HI there,
> >
> > Thanks to everyone who helped with my keyword
> problem - I think I thanked
> > them all individually but I thought I should
> mention it here too.
> >
> > Now, a new question:
> >
> > Does anyone know if there's a PHP class anywhere
> out there for paging
> > results from a PostgreSQL query, similar to
> Paginator or ezResults which
> do
> > that for MySQL? Or do I have to do the code for
> that from scratch?
> >
> > Alternatively, would it be difficult to adapt one
> of those to working with
> > PostgreSQL instead of MySQL?
> >
> >
> > Lynna
> > --
> > Resource Centre Database Coordinator
> > Gallery 44
> > www.gallery44.org
> >
> >
> > ---------------------------(end of
> broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to
> majordomo [at] postgresql.org
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 8: explain analyze is your friend
=3D=3D=3D=3D=3D
=C2ngelo Marcos Rigo
AMR Inform=E1tica
(51) 3348 0870
Rua Pe. Alois Kades 400/210
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo [at] yahoo.com.br
____________________________________________________________ ___________
Conhe=E7a o novo Cad=EA? - Mais r=E1pido, mais f=E1cil e mais preciso.
Toda a web, 42 milh=F5es de p=E1ginas brasileiras e nova busca por imagen=
s!
http://www.cade.com.br
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: Paging results
Do you have PEAR?
There is in
http://pear.php.net/package-info.php?package=3DDB_Pager,
the dbpager package, wich works very well
--- Lynna Landstreet <lynna [at] gallery44.org> escreveu: >
HI there,
>
> Thanks to everyone who helped with my keyword
> problem - I think I thanked
> them all individually but I thought I should mention
> it here too.
>
> Now, a new question:
>
> Does anyone know if there's a PHP class anywhere out
> there for paging
> results from a PostgreSQL query, similar to
> Paginator or ezResults which do
> that for MySQL? Or do I have to do the code for that
> from scratch?
>
> Alternatively, would it be difficult to adapt one of
> those to working with
> PostgreSQL instead of MySQL?
>
>
> Lynna
> --
> Resource Centre Database Coordinator
> Gallery 44
> www.gallery44.org
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
majordomo [at] postgresql.org
=3D=3D=3D=3D=3D
=C2ngelo Marcos Rigo
AMR Inform=E1tica
(51) 3348 0870
Rua Pe. Alois Kades 400/210
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo [at] yahoo.com.br
____________________________________________________________ ___________
Conhe=E7a o novo Cad=EA? - Mais r=E1pido, mais f=E1cil e mais preciso.
Toda a web, 42 milh=F5es de p=E1ginas brasileiras e nova busca por imagen=
s!
http://www.cade.com.br
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: Paging results
=C2ngelo Marcos Rigo wrote:
> Do you have PEAR?
>
getting started with pear:
root [at] home#lynx -source http://go-pear.org | php
you can run for test as normal user, than you have to configure the
variables bin_dir, doc_dir, data_dir and php_dir. Set this to your home,
you'll get a running pear-system.
greetings, Gerd
--
--------------------------------------------------------
# Gerd Terlutter | Mueller+Blanck Software GmbH #
# gerd [at] MplusB.de | Gutenbergring 38 #
# gerd.terlutter [at] web.de | D-22848 Noderstedt #
# tel:0171/6992579 | tel:+49 40 500 171-1 #
# Buero:040/500171-17 | fax:+49 40 500 171-71 #
--------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
Re: Paging results
=C2ngelo Marcos Rigo wrote:
> Do you have PEAR?
>
> There is in
> http://pear.php.net/package-info.php?package=3DDB_Pager,
> the dbpager package, wich works very well
>>Now, a new question:
>>
>>Does anyone know if there's a PHP class anywhere out
>>there for paging
>>results from a PostgreSQL query, similar to
>>Paginator or ezResults which do
>>that for MySQL? Or do I have to do the code for that
>>from scratch?
>>
>>Alternatively, would it be difficult to adapt one of
>>those to working with
>>PostgreSQL instead of MySQL?
>>
>>
>>Lynna
Hi all,
PEAR is general a good idea. I use PEAR::DB as wrapper for access do
different RDBMS, in my case PG and MySQL. nxet week i'll start with
transaction on MySQL3.23.51, but don't know what happends. Other
functionality is given during use PEAR::DB. This package supports 10 or
12 RDBMS. To convert MySQL to PG try this:
http://ns2.ziet.zhitomir.ua/~fonin/projects/my2pg/my2pg_man. html
Another good tipp is this:
http://www.rot13.org/~dpavlin/sql.html
If you have a good config-file for your source, you can switch by
changing only the param 'dbtype' e.g. psql or mysql.
excuse my bad english,
Gerd
--
--------------------------------------------------------
# Gerd Terlutter | Mueller+Blanck Software GmbH #
# gerd [at] MplusB.de | Gutenbergring 38 #
# gerd.terlutter [at] web.de | D-22848 Noderstedt #
# tel:0171/6992579 | tel:+49 40 500 171-1 #
# Buero:040/500171-17 | fax:+49 40 500 171-71 #
--------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
Re: Paging results
on 8/11/03 2:14 PM, =C2ngelo Marcos Rigo at angelo_rigo [at] yahoo.com.br wrote:
> Do you have PEAR?
>
> There is in
> http://pear.php.net/package-info.php?package=3DDB_Pager,
> the dbpager package, wich works very well
I'd never heard of it before - I'm fairly new to PHP. I just looked through
the documentation a bit, but I'm a bit confused - is this something I can
actually install on my own, in the gallery's home directory, or is it
something that our web host would have to install? We don't have our own
server; we're just a on a shared hosting plan.
Lynna
--
Resource Centre Database Coordinator
Gallery 44
www.gallery44.org
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
Re: Paging results
on 8/11/03 1:31 PM, =C2ngelo Marcos Rigo at angelo_rigo [at] yahoo.com.br wrote:
> I have this script (please adapt the section where the
> results are showed)
Thank you very much - I've implemented this on one of my results pages as a
test, and it seems to be partially working - the results are limited
properly, and there are no errors, but the next and previous links aren't
quite working right.
On the first page, the prev link points to offset 0 and page 1, which I
guess is correct, but the next link leaves offset blank, and has page 0,
instead of having offset 50 (that's what I set $limit to) and page 2 as I
would think it should.
If I enter those values manually in the URL -
http://www.gallery44.org/db/artists_browse_paged.php?offset= 3D50&pgnum=3D2 =
- the
second page displays correctly, but again the prev and next links don't work
right. The prev page here shows pgnum=3D1, which is correct, but it shows
offset as 30 when it should be 50. And the next link shows exactly what it
did on page 1 - offset=3D&pgnum=3D0. I've looked at the code, but I don't t=
hink
I'm following it well enough to be sure what's going wrong. I've attached a
copy of the file
I kept the code pretty much as it was except for the part where the results
are displayed, and a couple of very minor tweaks (changed the pg_query and
pg_num_rows to pg_exec and pg_numrows because I'm using PHP 4.1, changed the
part where it selects the whole table and uses pg_numrows to count it to a
select count because it seemed more efficient, and changed $conn to the
database connection I'd already opened, $db).
If you or anyone else could suggest what might be going wrong, I'd very much
appreciate it. Here's what I've got:
First part (before displaying query results):
// Paging script - request for parameters.
$offset =3D $_REQUEST['offset'];
$pgnum =3D $_REQUEST['pgnum'];
// Make that they are integer - security.
settype($offset, 'integer');
settype($pgnum, 'integer');
// Open database, i.e PostgreSQL
if (!$db) {
echo "An error occured - no database connection exists.\n";
exit;
}
// Initialize variables.
$limit=3D50; // rows to return
$numresults=3Dpg_exec("SELECT COUNT(*) FROM artists"); //PostgreSQL
// $numrows=3Dpg_numrows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=3D0;
$pgnum=3D1;
}
Then comes the display of the results, which is working fine.
Second part:
// calculate number of pages needing links
$pages=3Dintval($numrows/$limit);
// $pages now contains int of pages needed unless there is a
remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
echo "\n<p class=3D\"small\">";
// next we need to do the links to other results
if (pages!=3D1)
{
if ($pgnum=3D=3D1) {
print "<a href=3D\"$PHP_SELF?offset=3D0&pgnum=3D1\">PREV</a>
\n";
}
else
{
$prevoffset=3D$offset-20;
$cpgnum =3D intval($prevoffset/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$prevoffset&pgnum=3D$cpgnum\">PR EV</a> \=
n";
}
}
for ($i=3D1;$i<=3D$pages;$i++) { // loop thru
$newoffset=3D$limit*($i-1);
$cpgnum =3D $i;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">$cp gnum</a> =
\n";
}
// check to see if last page
if ($pages!=3D1)
{
if ($pgnum<$pages) {
$newoffset=3D$offset+$limit;
$cpgnum =3D intval(($offset+$limit)/$limit)+1;
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$cpgnum\">NEX T</a></p>\n";
}
else
{
print "<a
href=3D\"$PHP_SELF?offset=3D$newoffset&pgnum=3D$pages\">NEXT</a></p>\n";
}
}
Many thanks,
Lynna
--
Resource Centre Database Coordinator
Gallery 44
www.gallery44.org
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: Paging results
On Wed, 13 Aug 2003, Lynna Landstreet wrote:
> on 8/11/03 2:14 PM, =C2ngelo Marcos Rigo at angelo_rigo [at] yahoo.com.br wr=
ote:
>
> > Do you have PEAR?
> >
> > There is in
> > http://pear.php.net/package-info.php?package=3DDB_Pager,
> > the dbpager package, wich works very well
>
> I'd never heard of it before - I'm fairly new to PHP. I just looked thr=
ough
> the documentation a bit, but I'm a bit confused - is this something I c=
an
> actually install on my own, in the gallery's home directory, or is it
> something that our web host would have to install? We don't have our ow=
n
> server; we're just a on a shared hosting plan.
PEAR, and about 12 or so other packages, are usually distributed with PHP=
and found in the /usr/local/lib/php directory on most Unix boxes.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster