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:
10 results per page search Problem.

 



CuzDesign
Deleted

Jun 21, 2000, 2:54 PM

Post #1 of 3 (1053 views)
10 results per page search Problem. Can't Post

Hello,
I have looked over and studdied how to keep the results of a search from a database to show the first 10 and have the links to the other pages on the bottom, Previous and Next and so on... Here is the code I am using, any help would be 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;

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

Thank you in advance.

Ray


[This message has been edited by CuzDesign (edited 06-21-2000).]


rGeoffrey
User / Moderator

Jun 26, 2000, 2:21 PM

Post #2 of 3 (1053 views)
Re: 10 results per page search Problem. [In reply to] Can't Post

Below are some sections of the original code followed by an alternate version and some comments.

#original--------------------------------------
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;
}
#alternative-----------------------------------
unless (($string) &#0124; &#0124; ($mod)) {
print ("No data entered to search for.<br>\n",
qq [Please <a href="Javascript:history.back()">click here</a> to go back.\n]);
exit;
}
#comments--------------------------------------
Two things here.
By using the 'qq' we are able to use a double quote without the need for a back slash.
DeMorgan's Law allows us to change the conditional. The negative has moved outside and the && has become &#0124; &#0124;.
($string) is the same as ($string ne "").

#original--------------------------------------
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";
#alternative-----------------------------------
@columns = qw (Photo Stock Year Make Model Price Details);
print (qq [<table width="600" cellspacing="0" cellpadding="2" border="0"><tr bgcolor="#000000">\n],
(map { qq [<td><b><FONT SIZE="-1" FACE="verdana, arial, helvetica" color="#FFFFFF">$_</font></b></td>\n] } @columns),
"</tr>\n"
);
#comments--------------------------------------
The seven table cells are almost identical, by using a map with the different parts in the @columns array, this can be done
in one line. If you wanted to, you could avoid using @columns by using the qw () inside the print statement directly.
#original--------------------------------------
$dude = 0;
while (<DATA> ){
#stuff deleted here
if ($dude eq 0) { $color = '#D0D0D0';
$dude = 1;
}
else { $dude = 0;
$color = '#FFFFFF';
}
#alternative-----------------------------------
%nextColor = ('#D0D0D0' => '#FFFFFF',
'#FFFFFF' => '#D0D0D0');
$color = '#FFFFFF';
while (<DATA> ){
#stuff deleted here
$color = $nextColor{$color};
#comments--------------------------------------
Each time through the while loop, $color is used as the key in %nextColor. The trick here is that each key is used
as a value in the hash exactly once.


CuzDesign
Deleted

Jun 28, 2000, 11:33 PM

Post #3 of 3 (1053 views)
Re: 10 results per page search Problem. [In reply to] Can't Post

Thank you for your reply.

I can see how the code you typed will make a
better alternative for the codee I was using
thank you for taking the time out to show me
a shorter and more efficient way of doing those
things.

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