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: Advanced:
Help with search results problem!

 



CuzDesign
Deleted

Jun 21, 2000, 7:48 PM

Post #1 of 2 (2310 views)
Help with search results problem! Can't Post

Hello,
I am working on a program that searches a
database that contains some information on
cars. I have the program preatty much finished
except I am running into a problem with the
search part of it.

The problem I am having is only ouputing 10
results per page or so then having the Previous
and Next links at the bottom of the search results.

I can get it to print the results out to the
page for viewing and get the the Previous and
Next links to print however it prints all the
matches on one page not to different pages for
the links.

I have looked and studied the How to section
where there was an article on it but still
cannot seem to bet it to work properly. Here
is the code I am using.

<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;

$string = param('string');
$mod = param('mod');
if (($string eq "") && ($mod eq "")){
print "No data entered to search for.<br>\n";
print "Please <a href=\"Javascript:history.back()\">click here</a> to go back.\n";
exit;
}
$dude = 0;
print "<table width=\"600\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr bgcolor=\"#000000\">\n";
print "<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";
open (DATA,"<cars.dat") or die "Couldn't open cars.dat $!\n";
while (<DATA> ){
chomp;
@results = split(/:/,$_);
$result_count++;
if ($dude eq 0) { $color = '#D0D0D0';
$dude = 1;
}
else { $dude = 0;
$color = '#FFFFFF';
}
# 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 - 1) * $pagesize) + 1;
$lastresult = $firstresult + $pagesize - 1;
if ($lastresult > $result_count) {
$lastresult = $result_count;
}
if ($results[1] eq $string or $results[4] eq $mod){
$good = 1;
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 "<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=\"cgi-bin/results.pl?reqpage=$prev_page&pagesize=$pagesize\">" . "PREVIOUS" . "</a>";
}
if ($reqpage == $pagecount) {
$next_link = "";
} else {
$next_link = " <a href=\"cgi-bin/results.pl?reqpage=$next_page&pagesize=$pagesize\">" . "NEXT" . "</a>";
}
if ($pagecount > 1) {
$pagelinks = $prev_link;
$pageno = 0;
while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $reqpage) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"cgi-bin/results.pl?reqpage=$pageno&pagesize=$pagesize\">" . $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
} else {
$pagelinks = "";
}
}
}

close DATA;
print "<table>";
print "$pagelinks";
if ($good ne 1){
print "No entries found\n";
}
</pre><HR></BLOCKQUOTE>

Any help here would be greatly appreciated.

Thank you in advance.
Ray


DrZed
User

Jun 23, 2000, 5:56 PM

Post #2 of 2 (2310 views)
Re: Help with search results problem! [In reply to] Can't Post

The script is trying to use $result_count to determine how many pages there should be.

However, the value is being created during the whole listing process and it needs to be determined BEFORE the listing process and before it is used to determine pages.

Also, you calculate $firstresult and $lastresult, but don't use either.

Here's a version that should work, or at least be closer:

<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;
$string = param('string');
$mod = param('mod');
if (($string eq "") && ($mod eq "")){
print "No data entered to search for.<br>\n";
print "Please <a href=\"Javascript:history.back()\">click here</a> to go back.\n";
exit;
}
$dude = 0;
print "<table width=\"600\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr bgcolor=\"#000000\">\n";
print "<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";
open (DATA,"<cars.dat") or die "Couldn't open cars.dat $!\n";

@data=();
while (<DATA> ){
chomp;
$line=$_;
@results = split(/:/,$line);

if ($results[1] eq $string or $results[4] eq $mod){
push(@data,$line);
$result_count++;
}
}
close DATA;

# 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 - 1) * $pagesize) + 1;
$lastresult = $firstresult + $pagesize - 1;
if ($lastresult > $result_count) {
$lastresult = $result_count;
}

foreach ($firstresult..$lastresult ){
@results = split(/:/,@data[$_]);
if ($dude eq 0) { $color = '#D0D0D0';
$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 "<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=\"cgi-bin/results.pl?reqpage=$prev_page&pagesize=$pagesize\">" . "PREVIOUS" . "</a>";
}
if ($reqpage == $pagecount) {
$next_link = "";
} else {
$next_link = " <a href=\"cgi-bin/results.pl?reqpage=$next_page&pagesize=$pagesize\">" . "NEXT" . "</a>";
}
if ($pagecount > 1) {
$pagelinks = $prev_link;
$pageno = 0;
while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $reqpage) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"cgi-bin/results.pl?reqpage=$pageno&pagesize=$pagesize\">" . $pageno . "</a>";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
} else {
$pagelinks = "";
}

print "</table>";
print "$pagelinks";
if ($good ne 1){
print "No entries found\n";
}
</pre><HR></BLOCKQUOTE>

It's tough to follow the braces, but hopefully you will see what it's trying to do.

Just a suggestion, the following:

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

can be done more briefly as follows:
$pagecount = int(($result_count+$pagesize-1) / $pagesize);

Also, with the above code, you wouldn't need to tally $result_count because you could use $#data+1 instead. $#data+1 is the number of entries in the array @data.

And, rather than:
if ($dude eq 0) { $color = '#D0D0D0';
$dude = 1;
}
else { $dude = 0;
$color = '#FFFFFF';
}

you could simply do:
if ($color eq '#FFFFFF')
{ $color = '#D0D0D0'; }
else
{ $color = '#FFFFFF'; }

or, even briefer:
$color=(($color eq '#FFFFFF')?('#D0D0D0') Frown'#FFFFFF'));

Good luck,

Dr. Zed


 
 


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

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