Page 1 of 1

dynamic links for multiple pages?

Posted: Tue Jun 22, 2010 12:12 pm
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

Re: dynamic links for multiple pages?

Posted: Tue Jun 22, 2010 12:31 pm
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

Re: dynamic links for multiple pages?

Posted: Tue Jun 22, 2010 3:32 pm
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. :-)