Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pagenumbers searchresults

Status
Not open for further replies.

forces1

Programmer
Apr 5, 2007
29
NL
Hi all,

with the searchfunction on my website my script breaks up the results into pages with 10 results. You can click the other pagenumbers to visit the next pages. Now the problem is when I have for example 200 results, it will show the pagenumbers 1 till 20. That's not so good, I only want like 10 pagenumbers like Google uses. This is the code:

Code:
print "<table width='728' border=0 background='/images/categorie-onder728.gif' height='25' cellpadding='0' cellspacing='0'><tr><td class=bodytext align='right'>";

my $searchenc = $search;
$searchenc =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
$pagecount = $countresults / $linksperpage; # break the pages down

if ($pagecount =~ m/\./) {$pagecount = int ($pagecount + 1)}
  $view_page = $q->param("page");
  if ($view_page) {$page = $view_page} else {$page = 1}
     print "<font color='#ffffff'>[ $mltext5 </font>";
  for ($page_count = 1; $page_count <= $pagecount; $page_count++) {
     if ($page_count == $page) {
       print "<b><font color='#ffffff'> $page_count</font></b> ";
     } else {
       print "<a href=\"$searchscript?search=$searchenc&page=$page_count\">$page_count</a> ";
     }
}

print "<font color='#ffffff'>]</font></td><td width='2'></td></tr></table><br>";

Could you please help me to fix this? If it isn't clear what I want, please see the pagenumbers by Google for a better explanation ;)

Thank you!
 
Just create a sub-set of numbers for the links to show.

The logic:

- If we have >= 10 pages, 10 numbers should be shown
- Numbers can't be less than 1 or greater than the total number of pages

Something along the lines of...
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

my $pages = 50;
my $current = <STDIN>;

my $showMin;
my $showMax;

# lower number = our current page minus 5
$showMin = $current - 5;
$showMin = 1 if $showMin < 1;

# higher number = lower number + 10
$showMax = $showMin + 10;
$showMax = $pages if $showMax > $pages;

# Make sure we have at least 10 numbers to show.
if ($pages > 10) {
	if ($showMax - $showMin < 10) {
		$showMin = $showMax - 10;
	}
}

print "\$pages = $pages; \$current = $current\n";
print "[ ";
for (my $i = $showMin; $i <= $showMax; $i++) {
	print "$i ";
}
print "]\n";

Example output:
Code:
[kirsle@epsilon ~]$ perl pages.pl
1
$pages = 50; $current = 1
[ 1 2 3 4 5 6 7 8 9 10 11 ]

[kirsle@epsilon ~]$ perl pages.pl
5
$pages = 50; $current = 5
[ 1 2 3 4 5 6 7 8 9 10 11 ]

[kirsle@epsilon ~]$ perl pages.pl
10
$pages = 50; $current = 10
[ 5 6 7 8 9 10 11 12 13 14 15 ]

[kirsle@epsilon ~]$ perl pages.pl
25
$pages = 50; $current = 25
[ 20 21 22 23 24 25 26 27 28 29 30 ]

[kirsle@epsilon ~]$ perl pages.pl
40
$pages = 50; $current = 40
[ 35 36 37 38 39 40 41 42 43 44 45 ]

[kirsle@epsilon ~]$ perl pages.pl
46
$pages = 50; $current = 46
[ 40 41 42 43 44 45 46 47 48 49 50 ]

[kirsle@epsilon ~]$ perl pages.pl
50
$pages = 50; $current = 50
[ 40 41 42 43 44 45 46 47 48 49 50 ]

It may need a little bit of tweaking (i.e. it shows 1 through 11 on numbers less than 10), but that'll get you off to a good start.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Actually, change the <= to < in that for loop and you're all set.

Code:
for (my $i = $showMin; $i < $showMax; $i++) {

Code:
[kirsle@epsilon ~]$ perl pages.pl
1
$pages = 50; $current = 1
[ 1 2 3 4 5 6 7 8 9 10 ]

[kirsle@epsilon ~]$ perl pages.pl
5
$pages = 50; $current = 5
[ 1 2 3 4 5 6 7 8 9 10 ]

[kirsle@epsilon ~]$ perl pages.pl
10
$pages = 50; $current = 10
[ 5 6 7 8 9 10 11 12 13 14 ]

[kirsle@epsilon ~]$ perl pages.pl
20
$pages = 50; $current = 20
[ 15 16 17 18 19 20 21 22 23 24 ]

[kirsle@epsilon ~]$ perl pages.pl
50
$pages = 50; $current = 50
[ 40 41 42 43 44 45 46 47 48 49 ]

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
There are cpan modules that calculate this type of thing. But here's a quickly thrown together script that does what you want. Please note that google does more than just a 10 page listing. They have a delta of 10 for previous and 9 for next pages.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]POSIX[/green] [red]qw([/red][purple]ceil[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Readonly[/green][red];[/red]
[black][b]use[/b][/black] [green]URI::Escape[/green] [red]qw([/red][purple]uri_escape[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

Readonly [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$PAGE_DEFAULT[/blue] => [fuchsia]1[/fuchsia][red];[/red]
Readonly [black][b]my[/b][/black] [blue]$ITEMS_PER_PAGE[/blue] => [fuchsia]10[/fuchsia][red];[/red]
Readonly [black][b]my[/b][/black] [blue]$PAGING_UPPER_DELTA[/blue] => [fuchsia]9[/fuchsia][red];[/red]
Readonly [black][b]my[/b][/black] [blue]$PAGING_LOWER_DELTA[/blue] => [fuchsia]10[/fuchsia][red];[/red]

[gray][i]# Random Temporary Values[/i][/gray]
[black][b]my[/b][/black] [blue]$search[/blue] = [red]'[/red][purple]bob[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$page_current[/blue] = [fuchsia]15[/fuchsia][red];[/red] [gray][i]# $q->param("page");[/i][/gray]
[black][b]my[/b][/black] [blue]$result_count[/blue] = [fuchsia]252[/fuchsia][red];[/red]

[gray][i]# Setup Paging Results[/i][/gray]
[blue]$page_current[/blue] ||= [blue]$PAGE_DEFAULT[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$page_count[/blue] = [maroon]ceil[/maroon][red]([/red][blue]$result_count[/blue] / [blue]$ITEMS_PER_PAGE[/blue][red])[/red][red];[/red]
[black][b]my[/b][/black] [red]([/red][blue]$page_min[/blue], [blue]$page_max[/blue][red])[/red] = [maroon]get_page_range[/maroon][red]([/red][blue]$page_count[/blue], [blue]$page_current[/blue][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$searchenc[/blue] = [maroon]uri_escape[/maroon][red]([/red][blue]$search[/blue][red])[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$page[/blue] [red]([/red][blue]$page_min[/blue]..[blue]$page_max[/blue][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$page[/blue] == [blue]$page_current[/blue][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]qq{[/red][purple]($page) [/purple][red]}[/red][red];[/red]
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[black][b]print[/b][/black] [red]qq{[/red][purple]$page [/purple][red]}[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]get_page_range[/maroon] [red]{[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$total[/blue], [blue]$current[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
	
	[url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Error: Total less than current page [blue]$total[/blue] < [blue]$current[/blue][/purple][red]"[/red]
		[olive][b]if[/b][/olive] [blue]$total[/blue] < [blue]$current[/blue][red];[/red]
	
	[black][b]my[/b][/black] [blue]$min[/blue] = [blue]$current[/blue] - [blue]$PAGING_LOWER_DELTA[/blue][red];[/red]
	[blue]$min[/blue] = [fuchsia]1[/fuchsia] [olive][b]if[/b][/olive] [blue]$min[/blue] < [fuchsia]1[/fuchsia][red];[/red]
	
	[black][b]my[/b][/black] [blue]$max[/blue] = [blue]$current[/blue] + [blue]$PAGING_UPPER_DELTA[/blue][red];[/red]
	[blue]$max[/blue] = [blue]$total[/blue] [olive][b]if[/b][/olive] [blue]$max[/blue] > [blue]$total[/blue][red];[/red]
	
	[url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [red]([/red][blue]$min[/blue], [blue]$max[/blue][red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
Other Modules used :
[ul]
[li]Readonly[/li]
[li]URI::Escape[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top