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!

converting ASP to CGI?

Status
Not open for further replies.

ekimr

Technical User
Jun 28, 2002
124
GB
Hi,

Further to a previous thread I did, we have to pursue a line of sticking with a web hoster that doesn't do ASP and Databases.

We need to convert the database to be a flat file, then use CGI to access the Flat file.

We had all this working in ASP and now know zilch about CGI to re-do the solution. (creating the flat file causes us no problems)

We need to to enable customers to search the 'text file' desription field and return rows from the table that are a hit...

Is this possible in CGI and flat file or are we screwed !

Any help in starting (if its not tons of code obviously) would be appreciated...

Mike
 
Yes, you can do what you want.

You can pass to the script ( a .pl or a .cgi script ) either in a link or a form.

So your script would look something like

read the variable

open the flat file as an array

do a foreach loop and check the variable against a each record.

if there is a match then do something

Here is what some of the code might look like.

#!/usr/bin/perl

# this reads in the input string
# from a link like
$input_line = $ENV{QUERY_STRING};
$input_line =~ tr/+/ /;
@fields = split(/\&/,$input_line);
foreach $i (0 .. $#fields) {
($name,$value) = split(/=/,$fields[$i]);
$name =~ s/%(..)/pack("c",hex($1))/ge;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$input{$name} = $value;
}

# this opens the flat file
open(F, "../somefolder/yourdatafaile.txt");
@logfile = <F>;
close(F);

# this does a foreach loop checking
# the variable against the first field in each record
foreach (@logfile) {

($field1, $field2, $field3, $field4) = split (/\t/);

if ($input{'id'} eq $field1 ) {push @array, &quot;<tr><td>$field1</td><td>$field2</td><td>$field3</td><td>$field4</td></tr>\n&quot;; }

}

# this prints to the browser the serach results
print &quot;Content-type: text/html\n\n&quot;;

print <<END;

<html>
<head>
<title>Click Through Central</title>
</head>

<body bgcolor=&quot;#ffffff&quot;>
<center>Your search produced the following results:</center>
<table align=&quot;center&quot;>
@array
</table>
</body>
</html>

END

# end of script

I just wrote this off the top of my head, so it has not been tested, but something along this line will do what you want.

You can also read in search variable via an html form and do the samething as above. There would be more code, but the principle is the same. Also you can use .cgi or a .pl extension. I would just use a .pl extension.

hth...mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top