Subject: Paging with PHP & Postgresql
Posted By: ganesh15 Post Date: 8/13/2003 5:02:54 AM
Hi,

How to do paging with PHP using postgresql database. Any pointers or useful websites?

I browsed the web but mostly all the example are with php and mysql.

Regards,
ganesh

Reply By: ganesh15 Reply Date: 8/14/2003 10:41:49 PM
Please help. No response for my question until now.


Reply By: nikolai Reply Date: 8/18/2003 1:24:44 PM
Well, you'll need to limit the start row and the number of result rows returned by a SELECT query, based on which page you're on and how many results per page you wish to display.

To get the total number of pages, you'll need to know how many TOTAL rows are in the result set, even if you're not going to display them all.  In pseudocode:

row count = SELECT COUNT(*) FROM table

num pages = ceil(row count / rows per page)


Now, to display the page you're on:

first row = (page number - 1) * rows per page
  // this assumes the first page number is 1, not 0.

Now to select only that page's worth of rows, you use the LIMIT and OFFSET keywords in your query
SELECT * FROM table
 LIMIT {rows per page}
 OFFSET {first row)


As always, please RTM!!
  http://www.postgresql.org/
  http://www.postgresql.org/docs/7.3/interactive/queries-limit.html



Take care,

Nik
http://www.bigaction.org/
Reply By: ganesh15 Reply Date: 8/21/2003 1:17:26 AM
Thanks. I got paging to work.

Regards,
ganesh

Reply By: Buzz Reply Date: 9/10/2003 7:54:24 AM
Hi nikolai!

I read the pseudocode you wrote as an answer to ganesh15's question
and since i have the same prob i implemented your answer in my code but it wont work :(

This is what i wrote in my code:

$db = new DB_PSQL($db_host, $db_port, $db_name, $db_username, $db_password);

if (($u=='')&&($m=='')&&($i=='')) {
  $start= ($page number - 1) * $numrowsperpage;
  $query = "SELECT * FROM t_users ORDER BY id";
  $db->query($query);
  $kull = $query;
  $num_pages = ceil($kull / $numrowsperpage);
  $kull .=" LIMIT $numrowsperpage, $start";
}

Thought asking you if you may know whats wrong with it?

Thanks in advance
Regards,
Buzz

Reply By: nikolai Reply Date: 9/10/2003 1:15:55 PM
Well, why are you running your query before you tack on the "LIMIT" clause?

Also, I'm unclear as to why you set $kull = $query (a string), but then use $kull in a mathematical expression ($kull / $numrowsperpage).  $num_pages will ALWAYS be zero because PHP will convert the query string to 0 when it's used in a mathematical context.


I don't want to sound extra harsh, but you really need to take a few minutes and read each line of your code to make sure it makes sense.  The last three lines of your code block make absolutely no sense.  Line by line, this is how your program reads in english:

if $u and $m and $i are all empty strings,
  $start gets the value of the first row in this page.
  $query gets the value of an SQL query selecting ALL rows and columns.
  Run the query.
  $kull is a copy of the original query string.
  divide $kull (a string!) by $numrowsperpage, round up,
         and assign value to $num_pages.
  append LIMIT clause to $kull.


I know that learning a new language is difficult, but I cannot in good conscience just give you the corrected code.  I'll leave it to you to figure out.  If you're still having problems after giving it some serious thought, let us know and we'll see how we can guide you.


Take care,

Nik
http://www.bigaction.org/
Reply By: sumadusri Reply Date: 4/13/2006 11:40:27 PM
hi ganesh,

             I am also doing project on pHP,postgres.can u plz give
me the code for paging.As i am just now only learning PHp.



Go to topic 42920

Return to index page 314
Return to index page 313
Return to index page 312
Return to index page 311
Return to index page 310
Return to index page 309
Return to index page 308
Return to index page 307
Return to index page 306
Return to index page 305