Jump to content

"pages" of results...


DaveLinger

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?
Link to comment
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
Link to comment
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....
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.