Jump to content

Archived

This topic is now archived and is closed to further replies.

DaveLinger

"pages" of results...

Recommended Posts

so I have a script where a user selects from a dropdown box some search criteria, including the number of results to display per page (10, 20, 50, or 100; this number is stored in a variable). They press submit and it goes to the result page. I have it set to display the number of results the user specifies correctly, but I dont know how to get it to calculate how many pages there are total (results divided by number specified to display per page) and create links to them. I have it set to get the page number using GET (gosearch.php?page=1), I just need it to calculate the total number of pages, create the links (the links would be to the same page, but with ?page=pagenumber), and do the math to calculate which results to get.

For example, if the number per page were 10, and there are 54 results, I need PHP to create...

[code]Page 1<br>
<a href="gosearch.php?page=2">Page 2</a><br>
<a href="gosearch.php?page=3">Page 3</a><br>
<a href="gosearch.php?page=4">Page 4</a><br>
<a href="gosearch.php?page=5">Page 5</a><br>
<a href="gosearch.php?page=6">Page 6</a><br>
[/code]

and for the sql code, it would have to get the page number and number per page and calculate which results to get.

[code]SELECT * FROM my_table LIMIT 10,0[/code] for page 1, for example

[code]SELECT * FROM my_table LIMIT 10,10[/code] for page 2, for example

[code]SELECT * FROM my_table LIMIT 10,20[/code] for page 3, for example

(I guess that can be achieved by telling PHP to multiply $numberperpage x $page)

Anyone have any ideas?

Share this post


Link to post
Share on other sites
so if i understand correctly all you need is to do the math??
[code]
$pages = floor($total_results / $results_per_page) + 1
[/code]


the reason to add floor is to get rid of any decimals done during the divison... and then add one to it so that you can fit the "partial" page of results into the pagination.

need any more help let me know

Share this post


Link to post
Share on other sites
what about the math to do the last part, for ths SQL query? it would be $results_per_page * $current_page_number

Share this post


Link to post
Share on other sites
the way i would do it is to store the number left after each page number..

[code]
$resultsleft = $totalresults - $results displayed
$lastresult = $totalresults - $resultsleft
[/code]
make sense?

then your last query would be
[code]
select * from my_tbl limit  $lastresult,$totalresults;
[/code]

i think.. idk id have to try it....

Share this post


Link to post
Share on other sites
but uh... wouldn't I have to already do the sql statement to get the totalresults?

Share this post


Link to post
Share on other sites
calculating the total number of pages merely implies dividing the number of results by the number of results per page, and rounding UP (since the last page will serve the remainder).

[code]<?php
$per_page = 10;

$resource1 = mysql_query('SELECT COUNT(*) FROM table') or die(mysql_error());
$total_results = mysql_result($resource1, 0, 0);

$total_pages = ceil($total_results / $per_page);
?>[/code]

furthermore, the syntax for limit is:

[code]LIMIT offset, length[/code]

meaning that you setup what row you want to START at in the first number, and how many rows to grab in the second.  you'll always want to grab per_page number of rows, and you'll want to start on the current page MINUS ONE times per_page (because page 1 should start at 0, not per_page):

[code]$current_offset = (($current_page - 1) * $per_page);[/code]

all together now:

[code]<?php
$resource1 = mysql_query('SELECT COUNT(*) FROM table') or die(mysql_error());
$total_results = mysql_result($resource1, 0, 0);

$per_page = 10;
$total_pages = ceil($total_results / $per_page);
$current_page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$current_offset = ($current_page - 1) * $per_page;

$this_pages_results = "SELECT stuff FROM table LIMIT $current_offset, $per_page";
?>[/code]

i've used the ternary operator when assigning $current_page - it's the same as using an if-else statement, where:

[code](conditional) ? if_true : if_false[/code]

is equivalent to

[code]if (conditional) {
  if_true
} else {
  if_false
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.