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:
HELP! Search output shows extra page!

 



CuzDesign
Deleted

Nov 27, 2000, 7:38 AM

Post #1 of 5 (277 views)
HELP! Search output shows extra page! Can't Post

Hello,
I have a search script here that shows 10 results per page.

The problem I am having is that after I do a search if there are more than "1" found I get an
extra page of blank results on the "Previous" "1" "2" "3" "Next" links.

Example:

Matches found 30
First page chows right away
Then it is listed like so for the other 20 found.

1 2 3 Next

When it should only show:

1 2 Next

Below is the code I am using any help would be greatly appreciated.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(param);

$pagesize = 10;
$result_count = 0;
$reqpage = param('reqpage');
$string = param('string');
$good = 0;
open (DATA,"<cars.dat") or die "Couldn't open cars.dat $!\n";
@data=(DATA);
while (<DATA> ){
chomp;
$line=$_;
@results = split(/:/,$line);
if (lc($results[1]) == lc($string) or lc($results[4]) eq lc($string)){
push(@data,$line);
$good = 1;
$result_count++;

}
}
close (DATA);


print "<table width=\"600\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
print "<tr>\n<td colspan=7><FONT SIZE=\"-2\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">Total Results for $string: $result_count</font></td>\n</tr>\n";
print "<tr bgcolor=\"#000000\">\n<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Photo</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Stock</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Year</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Make</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Model</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Price</font></b></td>\n";
print "<td><b><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#FFFFFF\">Details</font></b></td>\n";
print "</tr>\n";

$pagecount = 0;
# Check to see how many to show on page
if ($result_count != 0) {
$pagecount = int($result_count / $pagesize);
if (($pagecount * $pagesize) != $result_count) {
$pagecount++;
}
}

$firstresult = ($reqpage * $pagesize) + 1;
$lastresult = ($firstresult + $pagesize) - 1;

if ($lastresult > $result_count) {
$lastresult = $result_count;
}
$dude = 0;
for ($firstresult..$lastresult){
@results = split(/:/,@data[$_]);
if (lc($results[1]) eq lc($string) or lc($results[4]) eq lc($string)){
if ($dude eq 0) {
$color = '#E2E2E2';
$dude = 1;
}else {
$dude = 0;
$color = '#FFFFFF';
}

if ($results[0] ne "none"){
print "<tr bgcolor=$color>\n";
print "<td align=\"center\"><a href=\"http://cuzzart.com/usedhtml/$stock.html\"><img src=\"http://cuzzart.com/usedhtml/images/camera.gif\" border=\"0\"></a></td>\n";
}else{
print "<tr bgcolor=$color>\n";
print "<td align=\"center\"><img src=\"http://cuzzart.com/usedhtml/images/none.gif\" border=\"0\"></td>\n";
}
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">", $results[1],"</font></td>\n";
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">", $results[2],"</font></td>\n";
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">", $results[3],"</font></td>\n";
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">", $results[4],"</font></td>\n";
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\">\$", $results[5],"</font></td>\n";
print "<td><FONT SIZE=\"-1\" FACE=\"verdana, arial, helvetica\" color=\"#000000\"><a href=\"http://cuzzart.com/usedhtml/$results[1].html\">Details</a></font></td>\n";
print "</tr>\n";
}
}
$prev_page = $reqpage - 1;
$next_page = $reqpage + 1;
if ($reqpage < 1) {
$prev_link = "";
} else {
$prev_link = " <a href=\"search.pl?reqpage=$prev_page&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . "PREVIOUS" . "</a>";
}

if ($reqpage == $pagecount) {
$next_link = "";
} else {
$next_link = " <a href=\"search.pl?reqpage=$next_page&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . "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=\"search.pl?reqpage=$pageno&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
}else{
$pagelinks = "";
}
print "</table>";
print <<HTML;
<table width="600" cellspacing="0" cellpadding="2" border="0">
<tr>
<td bgcolor="#000000"><div align="right"><font face="Verdana, Arial" size="-2">$pagelinks</font></td>
</tr>
</table>
HTML
if ($good ne 1){
print "No entries found\n";
}
</pre><HR></BLOCKQUOTE>

Thanks in advance

Ray


DrZed
User

Nov 27, 2000, 1:12 PM

Post #2 of 5 (277 views)
Re: HELP! Search output shows extra page! [In reply to] Can't Post

Your code seems right for what it's doing... your expectations are different.

The list isn't showing the "next" pages, it's giving a link to all pages. For 30 entries, there are 3 pages, so you get 1, 2 and 3.

If you are on page 1, then "1" won't be a link and the others would. Same for the other pages.

If you want to remove the number for the current page, simply change the following:

if ($pageno == $reqpage) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"search.pl?reqpage=$pageno&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . $pageno . "</a>";
}

to

if ($pageno != $reqpage) {
$thislink = " <a href=\"search.pl?reqpage=$pageno&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . $pageno . "</a>";
}

However, I wouldn't change it at all. It's operating correctly.


CuzDesign
Deleted

Nov 27, 2000, 1:44 PM

Post #3 of 5 (277 views)
Re: HELP! Search output shows extra page! [In reply to] Can't Post

Thanks for the reply.

Yes everything works fine except I don't get 1,2,3 and page one is not linked. I get 1,2,3,4 and all are linked.

Example:

I do a search for 12345 and 40 matches are found.

Now on the first page I get the first 10 matches, and on the bottom it should show.

1,2,3,4, Next and the 1 should not be linked.

But what I get is 1,2,3,4, Next and all are linked including the 1.

If I click the 4 I get the fourth page but there are no results because right now it is displaying the first results as page "0".

What I can't figure out is how do I get it where the first page is indeed the "1" instead of "0" cause "0" never shows. and the "4" is a blank search result.

Basically it is like so but not shown.

0,1,2,3,4, Next - All are linked

When it should be
1,2,3,4, Next - Only 2,3,4, Next should be linked.


I have it setup so it will show the pages and the "Previous and "Next" links as well.


Any help in this would be greatly appreciated.

regards,
Ray


DrZed
User

Nov 28, 2000, 8:31 PM

Post #4 of 5 (277 views)
Re: HELP! Search output shows extra page! [In reply to] Can't Post

Ah, well I would think that the quick fix would be to change....

while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $reqpage) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"search.pl?reqpage=$pageno&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}

.... into this ....

while ($pageno < $pagecount) {
if ($pageno == $reqpage) {
$thislink = " <b>++$pageno</b> ";
} else {
$thislink = " <a href=\"search.pl?reqpage=$pageno&pagesize=$pagesize&string=$string\"><font color=\"#FFFFFF\">" . ++$pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}

The number in the URL will count from 0, but the links will count from 1.... This may seem a bit quirky, but it's the easiest fix.


CuzDesign
Deleted

Nov 29, 2000, 8:41 AM

Post #5 of 5 (277 views)
Re: HELP! Search output shows extra page! [In reply to] Can't Post

DrZed,
Thanks for the replies. The last solution didn't work but I found a way to not have the last blank page.

Here is where I fixed the problem.

I change the below code from this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($result_count != 0) {
$pagecount = int($result_count / $pagesize);
if (($pagecount * $pagesize) != $result_count) {
$pagecount++;
}
}
</pre><HR></BLOCKQUOTE>

To this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($result_count != 0) {
$pagecount = int($result_count / $pagesize) - 1;
if (($pagecount * $pagesize) != $result_count) {
$pagecount++;
}
}
</pre><HR></BLOCKQUOTE>

By adding the - 1 to the end of the int($result_count / $pagesize); I was able to get ride of the extra page.

Thanks for the help.

Regards,
Ray

 
 


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

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