Thursday 24 April 2008

Creating Pagination for my search results

Pagination is where in receiving search results you have too many to list on one page. So therefore you would like to have some sort of links system where you can click on '<>' and get the next 10 or however many you wish.

to solve this in my search results I had tried downloading other peoples classes but after some head scratching and figuring out could not adapt them to the search script I had already wrote. In the end I decided it'd be better if I wrote the code myself. Here is how I reached my final aim - I am hoping to write this in such a way that it may help others try to get there heads around how this will need to work for them.

In my script I will have the value $limit - this is the amount of results per page that we want per page.

the selectquery runs the search - Yours will be specfic to yourselves

from that selectquery we need to find out how many results there are.
[code]
$numresults=mysql_query($selectquery);
$numrows=mysql_num_rows($numresults);

[/code]

Next I want to find out if there is indeed more results that will fit onto 1 page and if so how many pages are there in total

[code]
if ($numrows > $limit){ // this calculates whether theres enough results to have pagination
$pagesExact = $numrows / $limit; // how many pages?

$pages = ceil($pagesExact); // gives the next highest round figure
}
[/code]

In the future at this stage in the script it may be the case that we are already on another page of search results. What I mean is that at the top of my search script I will check to see what page of the search this. I am going to send this to further pages in the URL using $_GET

>> $currpage = $_GET['currPage'];

Next I'll check to see if there is already a $currPage value - if not I'll take it as red were on page 1.

[code]
if (!$currPage) {
$currPage = 1;

}
[/code]

At this point in code I want to get the results that I am going to display. So that I only display the set that the page requires I am going to use the mysql command of LIMIT and OFFSET.
adding LIMIT to mysql will only give you that number of rows. By using OFFSET then we can offset the search ie by using an offset value of 10 then thats the row it will start at. Consider..

[code]

if ($currPage > 1){
$offset = ($currPage-1) * $limit;

$selectquery .= " LIMIT $limit OFFSET $offset ";
}else {
$selectquery .= " LIMIT $limit ";
}
[/code]

what I've done here is that if the search is more than 1 then I run the query with an offset. I calculated that the currpage ( example =2) take away 1 (example =1) times the limit (example 10) will equal (example 10) the offset that we need.

Now here comes the code to show the previous page if we need it
[code]

if ($currPage>1) {
$nPage=$currPage - 1;

print "\<a href="$PHP_SELF?currpage="$nPage\" wordsearch="TRUE&find="><<Prev 10&nbsp ";
}

[/code]

firstly we make it display if there is more than 1 page here to display. $currPage>1

Next we give the link the right number so when redirecting back to this script we know what page we're on. $nPage=$currPage - 1


The code for next>> page is similar

[code]

if ($currPage < $pages) { $nPage=$currPage + 1;

print " \<a href="$PHP_SELF?currpage="$nPage\" wordsearch="TRUE&find=">Next 10 >>";
}

[/code]

but we add a page to $nPage.

Next we give details of how many searches are being shown and how many results there are.

[code]

if ($currPage==1){

if ($offset > $numrows){

$offset = $numrows;
}

echo "

Showing results $currPage to $offset of $numrows

";
}
else {

$countresults = ($currPage - 1) * $limit; // we need to take away 1 from current page as page 3 needs to return results 20-30

$countAdd10 = $countresults + 10;
if ($countAdd10 > $numrows){

$countAdd10 = $numrows;}echo "

Showing results $countresults to $countAdd10 of $numrows

";
}

}
[/code]

And thats it. Any suggested improvements let me know.

No comments: