Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 13th, 2003, 05:02 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Paging with PHP & Postgresql

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 With Quote
  #2 (permalink)  
Old August 14th, 2003, 10:41 PM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please help. No response for my question until now.


Reply With Quote
  #3 (permalink)  
Old August 18th, 2003, 01:24 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/i...ies-limit.html



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old August 21st, 2003, 01:17 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I got paging to work.

Regards,
ganesh

Reply With Quote
  #5 (permalink)  
Old September 10th, 2003, 07:54 AM
Registered User
 
Join Date: Sep 2003
Location: Skelleftea, , Sweden.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Buzz
Default

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 With Quote
  #6 (permalink)  
Old September 10th, 2003, 01:15 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #7 (permalink)  
Old April 13th, 2006, 11:40 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php and postgresql crmpicco PHP Databases 0 February 23rd, 2007 08:34 AM
new table cols per record, paging, & select scottiegirl PHP Databases 5 June 21st, 2006 12:29 PM
begin php & mysql - chapter 12, user_form.php jon_stubber Beginning PHP 1 March 9th, 2006 10:57 AM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM
PHP4, Javascript & PostgreSQL ejr1959 PHP Databases 1 September 10th, 2003 12:52 PM



All times are GMT -4. The time now is 03:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.