dynamic links for multiple pages?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

dynamic links for multiple pages?

Post by iansane »

Hi,

Basically what I need to do is the same as the forum pages hear where it says "page 1 of 100 pages" and then has the 1,2,3,4,5...100 links to go to the pages.

I will be querying a database with several hundred records and displaying them 10 or 20 records to a page and need a way for the user to click to the next page.

I have an idea how I can do that by using mysql_num_rows() to get the count and then a for loop to create the row of links but the actual pages is a problem.

Are they actually separate pages or just echoed out information in the same page that changes when the link is clicked? And is there a lot to this or is there a known script that will do the job for me?

Thanks
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: dynamic links for multiple pages?

Post by Jade »

What you have to do is setup your query so that it limits the number of results it returns. Then each "page" is a part of the entire result set.

Code: Select all


$num_results_per_page = 50;
$total_rows = mysql_num_rows(mysql_query("SELECT * FROM tablename")) or die ('cannot get total results');

$low = $_GET['page'] * $num_results_per_page;
$high = ($_GET['page'] * num_results_per_page) + num_results_per_page;

$loop = mysql_query("SELECT * FROM tablename LIMIT $low, $high) or die ('cannot get results for this page');

while ($row = mysql_fetch_array($loop))
{
  //list all of your results here
}

//now generate your links to all of the different pages
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: dynamic links for multiple pages?

Post by iansane »

Thanks Jade,

That gives me a good bit of information to get started with. I'll see if I can get it all figured out from here. :-)
Post Reply