CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How do I limit search results to 10 hits per page?

 



ahowarth
Deleted

Apr 3, 2000, 8:04 AM

Post #1 of 4 (912 views)
How do I limit search results to 10 hits per page? Can't Post

I have a flat file database that I created
and I want to return results from a search (no problem), BUT I only want to show 10 results per page and have a NEXT BUTTON to get to the next page of results. Can anyone please give me some advice?

The database looks something like this:

name|age|photograph
name|age|photograph
name|age|photograph

thanks,

Anthony


RayStreet
Deleted

Apr 3, 2000, 9:48 PM

Post #2 of 4 (912 views)
Re: How do I limit search results to 10 hits per page? [In reply to] Can't Post

Assuming that you know the total number of hits (in $hittotal) and the results per page (in $pagesize) then you can do something like the following.

if ($hittotal != 0) {
$pagecount = int($hittotal / $pagesize);
if (($pagecount * $pagesize) != $hittotal) {
$pagecount++;
}
}

This works out the total number of pages that can be shown (in $pagecount).

You then output the first 10 results (handling less than 10 results if that condition arises).

After that you can put href links for each possible page value - you can put "Previous" and/or "Next" links as well - and can make the current page number (in $reqpage) a non-link so the user knows where they are. Something like the following:

$prev_page = $reqpage - 1;
$next_page = $reqpage + 1;
if ($reqpage == 1) {
$prev_link = "";
} else {
$prev_link = " <a href=\"http://domain/cgi-bin/?page=$prev_page&pagesize=$pagesize\">" . "«PREVIOUS" . "</a> ";
}
if ($reqpage == $pagecount) {
$next_link = "";
} else {
$next_link = " <a href=\"http://domain/cgi-bin/?&page=$next_page&pagesize=$pagesize\">" . "NEXT»" . "</a>";
}

$pagelinks = $prev_link;
$pageno = 0;
if ($pagecount > 1) {
while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $reqpage) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"http://domain/cgi-bin/?page=$pageno&pagesize=$pagesize\">" . $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
print <<endHTML5;
<table width="620" border="0">
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">$pagelinks<br><br></font></td>
</tr>
</table>
endHTML5

}

You can check at the top of your script to see if reqpage is an input parameter - if it is then use it to work out which page number you want otherwise default the page number to 1.

You multiply the $reqpage value by the $pagesize value and then add 1 to get the first result to show.

Hope this helps.


RayStreet
Deleted

Apr 3, 2000, 9:50 PM

Post #3 of 4 (912 views)
Re: How do I limit search results to 10 hits per page? [In reply to] Can't Post

By the way, it's tricky to get the spacing between the links and a button correct to make the link line look good which is why I went for the text option of "Previous" and "Next".


ahowarth
Deleted

Apr 4, 2000, 6:56 AM

Post #4 of 4 (912 views)
Re: How do I limit search results to 10 hits per page? [In reply to] Can't Post

Thanks Darian and RayStreet for your input.
It was very helpful. All is up and running
smoothly.

Anthony

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives