Jump to content


Photo

"pages" of results...


  • Please log in to reply
5 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 16 July 2006 - 04:13 AM

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...

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>

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

SELECT * FROM my_table LIMIT 10,0
for page 1, for example

SELECT * FROM my_table LIMIT 10,10
for page 2, for example

SELECT * FROM my_table LIMIT 10,20
for page 3, for example

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

Anyone have any ideas?
http://www.DaveLinger.com
dave at linger dot com

#2 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 16 July 2006 - 04:19 AM

so if i understand correctly all you need is to do the math??
$pages = floor($total_results / $results_per_page) + 1


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

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 16 July 2006 - 04:24 AM

what about the math to do the last part, for ths SQL query? it would be $results_per_page * $current_page_number
http://www.DaveLinger.com
dave at linger dot com

#4 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 16 July 2006 - 04:30 AM

the way i would do it is to store the number left after each page number..

$resultsleft = $totalresults - $results displayed
$lastresult = $totalresults - $resultsleft
make sense?

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

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

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 16 July 2006 - 04:38 AM

but uh... wouldn't I have to already do the sql statement to get the totalresults?
http://www.DaveLinger.com
dave at linger dot com

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 July 2006 - 05:13 AM

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).

<?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);
?>

furthermore, the syntax for limit is:

LIMIT offset, length

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):

$current_offset = (($current_page - 1) * $per_page);

all together now:

<?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";
?>

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

(conditional) ? if_true : if_false

is equivalent to

if (conditional) {
  if_true
} else {
  if_false
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users