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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI and Date File

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
US
I have written the following program. I want to read all the records and test each record for a match, and print it. However, if a match is not found, it should print out no match found at the end of reading the whole file. I get the result each record at a time, but I would like to print no match found if no record is found. Please help.

#!/usr/bin/perl

print "Content-type:text/html\n\n";

use CGI;

$query = new CGI;

use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);

my $make1 = param("make");
my $model1 = param("model");
my $mile1 = param("mile");
my $year1 = param("year");
my $price1 = param("price");


eval {
open(GUEST, "<car.out") or dienice("Couldn't open auto.out for reading: $! \n");
while (<GUEST>) {
chomp;
($make,$photo,$model,$color,$mile,$eyear,$price=split/\|/;

if ($make1 eq $make) {
if ( $model1 eq $model) {
if($year1 eq $eyear) {
if($price1 le $price) {
if($mile1 le $mile) {


if ($photo eq 'No') {
print "<table width=\"100%\">\n";
print "<tr bgcolor=\"red\"><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>\n";
print "<th>Price</th><th>E-Mail</th><th>Telephone</th><th>Time-to-Call</th></tr>\n";
print "<td align=\"left\" width=\"50\">$eyear</td><td align=\"left\" width=\"65\"><font color=\"blue\"> $make </font></td><td align=\"left\" width=\"50\">$model</td><td align=\"left\" width=\"50\">$color</td><td align=\"left\" width=\"50\">$mile</td>\n";
print "<td align=\"left\" width=\"50\">$d$price</td><td align=\"left\" width=\"50\"><a href=\"mailto: $email\" >$email </a></td>
<td align=\"left\" width=\"50\">$tele</td><td align=\"left\" width=\"50\"> $time</td>\n";
print "<tr align=\"left\" valign=\"top\" bgcolor=\"#FF99FF\"> <td align=\"left\" colspan=\"7\" >$comment1</td>\n";
print "<td align=\"left\" colspan=\"2\"><font color=\"blue\">CODE#$tele $month-$day-$year</font></td></tr></table>\n";
}
}
}
}
}

else { print "test\n"; }
}
}
}


#if ($yes eq '0' ) { print " Your specified car is not available, please go back and list the whole file\n"; }
 
You haven't placed a "No match" anywhere in the output. Also, you need to define a bunch of variables as well. But I'd take one step at a time and just work with the ones pertaining to the cars. One you've got it working, you can go back and add them in.

I've taken the liberty of making some changes to your script to make it a little more 'readable'.
Code:
#!/usr/bin/perl

#use CGI;       [COLOR=red]don't need this[/color]  
#$query = new CGI; [COLOR=red]not using this[/color]

use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);

my $make1  = param("make");
my $model1 = param("model");
my $mile1  = param("mile");
my $year1  = param("year");
my $price1 = param("price");

eval {
open(GUEST, "<car.out") or dienice("Couldn't open auto.out for reading: $! \n");
  while (<GUEST>) {
  chomp;
 ($make,$photo,$model,$color,$mile,$eyear,$price=split/\|/);

if ($make1 eq $make && $model1 eq $model && $year1 eq $eyear && $price1 le $price && $mile1 le $mile) {

print "Content-type:text/html\n\n";

if ($photo eq 'No') {
print<<EOF;  [COLOR=red]this lets you write html without escaping special characters[/color]
<html>
<body>
<table width="100%">
<tr bgcolor="red"><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>
<th>Price</th><th>E-Mail</th><th>Telephone</th><th>Time-to-Call</th></tr>
<td align="left" width="50">$eyear</td><td align="left" width="65"><font color="blue"> $make </font></td><td align="left" width="50">$model</td><td align="left"  width="50">$color</td><td align="left"  width="50">$mile</td>
<td align="left" width="50">$d$price</td><td align="left"  width="50"><a href="mailto: $email" >$email </a></td>
<td align="left" width="50">$tele</td><td align="left"  width="50"> $time</td>
<tr align="left" valign="top" bgcolor="#FF99FF"><td  align="left" colspan="7">$comment1</td>
<td align=left" colspan="2"><font color="blue">CODE#$tele $month-$day-$year</font></td></tr></table>;
</body>
</html>
EOF
}
else {print "test";}
}
else {print "No match";}   [COLOR=red] NO MATCH![/color]
}

You'd also be doing yourself a favor by putting use strict; right after your shebang as well as checking the syntax of your code. The command line for checking syntax is perl -c <filename>. Using this will save you a ton of headaches.

There's always a better way. The fun is trying to find it!
 
I get error with your program and it does not solve the problem. Thank you.
 
What is the error?

There's always a better way. The fun is trying to find it!
 
Here are the errors:

syntax error at carsearch2.cgi line 37, near "you write"
Might be a runaway multi-line << string starting on line 24)
Missing right curly or square bracket at carsearch2.cgi line 42, at end of line
syntax error at carsearch2.cgi line 42, at EOF
Execution of carsearch2.cgi aborted due to compilation errors.

 
Sorry - my fault... here's the revised script.

Code:
#!/usr/bin/perl

#use CGI;       don't need this  
#$query = new CGI; not using this

use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);

my $make1  = param("make");
my $model1 = param("model");
my $mile1  = param("mile");
my $year1  = param("year");
my $price1 = param("price");

eval {
open(GUEST, "<car.out") or dienice("Couldn't open auto.out for reading: $! \n");
  while (<GUEST>) {
  chomp;
 ($make,$photo,$model,$color,$mile,$eyear,$price=split/\|/;

if ($make1 eq $make && $model1 eq $model && $year1 eq $eyear && $price1 le $price && $mile1 le $mile) {

print "Content-type:text/html\n\n";

if ($photo eq 'No') {
print<<EOF;  this lets you write html without escaping special characters
<html>
<body>
<table width="100%">
<tr bgcolor="red"><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>
<th>Price</th><th>E-Mail</th><th>Telephone</th><th>Time-to-Call</th></tr>
<td align="left" width="50">$eyear</td><td align="left" width="65"><font color="blue"> $make </font></td><td align="left" width="50">$model</td><td align="left"  width="50">$color</td><td align="left"  width="50">$mile</td>
<td align="left" width="50">$d$price</td><td align="left"  width="50"><a href="mailto: $email" >$email </a></td>
<td align="left" width="50">$tele</td><td align="left"  width="50"> $time</td>
<tr align="left" valign="top" bgcolor="#FF99FF"><td  align="left" colspan="7">$comment1</td>
<td align=left" colspan="2"><font color="blue">CODE#$tele $month-$day-$year</font></td></tr></table>;
</body>
</html>
EOF
}
else {print "test";}
}
else {print "No match";}    NO MATCH!
}}}[\code]

There's always a better way.  The fun is trying to find it!
 
I fixed the problem and now it works. However, it does not do what I wanted it to do. That is, it prints the no match message in the loop. But I wanted the program to finish searching the file and then if it does not find a match should print no match.
 
The logic of this script won't allow this type of search because it will terminate at the first record that doesn't match. You could set a varaiable to 0 and if it does find a record that doesn't match, the variable is set to 1. When you reach the end of the file you would test the value of this variable - if 1 then print "No match".

You'd be far better ahead to use a database that you can apply a search query to. Depending on how you write the search criteria, if no records were returned then you would print "No match". Otherwise, all records would be printed.

There's always a better way. The fun is trying to find it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top