Click to See Complete Forum and Search --> : My Code So Far


MrNewbie
02-23-2001, 04:39 PM
Here is my code so far for my database program, its probably really messy and I'm sure theres better ways of doing a lot of it, but I like it so far, it works and I'll concentrating on making it more effieient and better after it works. Its really not finished yet though as a few of the functions are missing. I'll also remove some of the hardcoding once it works too.


#!/usr/bin/perl
use strict;
use CGI qw(:standard);

my $action;

$action = param('action');

if($action eq 'enterrec')
{
print header();

print "<HTML><HEAD><TITLE>Add Video</TITLE></HEAD>\n";
print "<body bgcolor=\"#000000\" background=\"../bkgnd.gif\" text=\"#FF0000\" link=\"#E4B407\" alink=\"#E4B407\" vlink=\"#E4B407\">";
print "<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>\n<p align=\"center\">";
print "<font face=\"Arial\" size=\"3\"><b>Enter Video Details To Add To Database:</b></font></p><p> </p>";
print "<p align=\"center\">\n<form action=\"vbase.pl\" method=\"post\">\n";
print "<font face=\"Arial\" size=\"2\"><b>";
print "<input type=\"hidden\" value=\"addrec\" name=\"action\">\n";
print "Title: <INPUT TYPE=\"text\" NAME=\"title\" SIZE=\"30\"><br><br>\n";
print "Genre: <INPUT TYPE=\"text\" NAME=\"genre\" SIZE=\"30\"><br><br>\n";
print "Rating: <INPUT TYPE=\"text\" NAME=\"rating\" SIZE=\"30\"><br><br>\n";
print "Year: <INPUT TYPE=\"text\" NAME=\"year\" SIZE=\"30\"><br><br>\n";
print "Director: <INPUT TYPE=\"text\" NAME=\"director\" SIZE=\"30\"><br><br>\n";
print "Stars:<br><TEXTAREA NAME=\"stars\" ROWS=\"4\" COLS=\"30\"></TEXTAREA><br><br>\n";
print "Available (Checked = Yes): <INPUT TYPE=\"checkbox\" NAME=\"av\" VALUE=\"checked\"></b></font><br><br>\n";
print "<INPUT TYPE=\"image\" SRC=\"../submit.gif\" BORDER=\"0\" ALT=\"Submit!\">\n";
print "</form>\n";

print end_html;
}
elsif($action eq 'addrec')
{
my $title = param('title'); #Sets title parameter to variable title.
my $genre = param('genre'); #Sets genre parameter to variable genre.
my $rating = param('rating'); #Sets rating parameter to variable rating.
my $year = param('year'); #Sets year parameter to variable year.
my $director = param('director'); #Sets director parameter to variable director.
my $stars = param('stars'); #Sets stars parameter to variable stars.
my $av = param('av'); #Sets availability paremeter to variable av.
&addrec($title, $genre, $rating, $year, $director, $stars, $av);
}
elsif($action eq 'delrec')
{
my $title = param('title');
&delrec();
}
elsif($action eq 'search')
{
my $sstring = param('sstring');
&search($sstring);
}
elsif($action eq 'modrec')
{
my $title = param('title');
&modrec();
}
else{

print header();

print "<HTML><HEAD><TITLE>Search For Video</TITLE></HEAD>\n";
print "<body bgcolor=\"#000000\" background=\"../bkgnd.gif\" text=\"#FF0000\" link=\"#E4B407\" alink=\"#E4B407\" vlink=\"#E4B407\">";
print "<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>\n<p align=\"center\">";
print "<font face=\"Arial\" size=\"3\"><b>Enter Film Title To Search For:</b></font></p><p> </p>";
print "<p align=\"center\">\n<form action=\"vbase.pl\" method=\"post\">\n";
print "<input type=\"hidden\" value=\"search\" name=\"action\">\n";
print "<INPUT TYPE=\"text\" NAME=\"sstring\" SIZE=\"30\"><br><br>\n";
print "<INPUT TYPE=\"image\" SRC=\"../submit.gif\" BORDER=\"0\" ALT=\"Submit!\">\n";
print "</form>\n";

print end_html;
}


sub addrec{

my $av;
my($title, $genre, $rating, $year, $director, $stars, $avr) = @_;

if($avr eq 'checked'){
$av = 1;
}
else{
$av = 0;
}

if (open(VDB, ">>vbase.db"))
{
printf(VDB "%s\t%s\t%s\t%s\t%s\t%s\t%d\n", $title, $genre, $rating, $year, $director, $stars, $av);
close VDB;

print header();

print "<HTML><HEAD><META HTTP-EQUIV=\"refresh\" content=\"10;URL=http://www.super8video.f2s.com/cgi-bin/vbase.pl?action=enterrec\">";
print "<TITLE>Record Added!</TITLE></HEAD>\n";
print "<body bgcolor=\"#000000\" background=\"../bkgnd.gif\" text=\"#FF0000\" link=\"#E4B407\" alink=\"#E4B407\" vlink=\"#E4B407\">";
print "<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>\n<p align=\"center\">";
print "<font face=\"Arial\" size=\"3\"><b>Film Record Added!</font><br>";
print "<font face=\"Arial\" size=\"2\">Taking You Back To Add A Record Menu In 10 Seconds</b></font></p><p> </p>";

print end_html;

}
else{

close VDB;

print header();

print "<HTML><HEAD><META HTTP-EQUIV=\"refresh\" content=\"10;URL=http://www.super8video.f2s.com/cgi-bin/vbase.pl?action=enterrec\">";
print "<TITLE>Failed Adding Record</TITLE></HEAD>\n";
print "<body bgcolor=\"#000000\" background=\"../bkgnd.gif\" text=\"#FF0000\" link=\"#E4B407\" alink=\"#E4B407\" vlink=\"#E4B407\">";
print "<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>\n<p align=\"center\">";
print "<font face=\"Arial\" size=\"3\"><b>File Opening Failed!</font><br>";
print "<font face=\"Arial\" size=\"2\">Taking You Back To Add A Record Menu In 10 Seconds</b></font></p><p> </p>";

print end_html;
}
}

sub search{

my ($line, $sstring, $first, $title, $genre, $rating, $year, $director, $stars, $av);
my @record;

$sstring = $_[0];
$sstring = lc($sstring);
}


Basically its an online database in perl for a school project but there some things I just cant get working. Mainly the search part. (The $sstring is being made lowercase for a comparison with the film title which will also be made lowercase for the comparison.)
I tried something like this to get the search to work (It searches by film title).


sub search{

my ($line, $sstring, $first, $title, $genre, $rating, $year, $director, $stars, $av);
my @record;

$sstring = $_[0];
$sstring = lc($sstring);

if(open(FILE, "vbase.db")){

while($line=<FILE> )
{
($first) = split(/\t/,$line);
if($first eq $sstring){
(@record) = split(/\t/,$line);
($title, $genre, $rating, $year, $director, $stars, $av) = @record;
print "@record";
}
}
}
}


I guess I'm overlooking something but I can't figure out what it is. Can any of you suggest what I've missed?

jemfinch
02-23-2001, 06:09 PM
You, my friend, need to learn how to print multiline strings.


print <<__EOF__
This is one line.
THis is the second line.
This is the last line.
___EOF___


Jeremy

MrNewbie
02-23-2001, 06:18 PM
Haha for all the HTML stuff? Yeah I should and probably will convert it to that. I will also be adding a lot of comments. I don't think I need to do the \'s before the "'s if I did it like that too right? I've been trying for literally hours to get that search thing to work and I still can't, the file is something like this:


Film1 Genre1 Rating1 Year1 Director1 Stars1 1
Film2 Genre2 Rating2 Year2 Director2 Stars2 0
Film3 Genre3 Rating3 Year3 Director3 Stars3 1


Etc. But it seems when I read the first field from each line (To try to compare it with the title submitted) it reads the first Field of ALL records. What can I do to stop this?
Thanks
MrNewbie

TheLinuxDuck
02-23-2001, 06:50 PM
The only thing I can suggest upon a quick glance of your code is to make sure you do some error checking for the param variables.

$action = param('action');


If that param call doesn't define anything to $action, the script will not be happy. :)

You can change it set a default if it doesn't return anything like:


my($action)=param('action') || "enterrec";


:)

[ 23 February 2001: Message edited by: TheLinuxDuck ]

TheLinuxDuck
02-23-2001, 06:52 PM
Originally posted by jemfinch:
You, my friend, need to learn how to print multiline strings.

print <<__EOF__
This is one line.
THis is the second line.
This is the last line.
___EOF___



BAH!!!! I can accomplish the same thing using print regular style, and still maintain nice formatting.


print "This is one line.\n",
"This is the second line.\n",
"This is the last line.\n";


I've never liked <<'' print formatting.

MrNewbie
02-23-2001, 09:25 PM
Ok I modified it slightly, heres the new version:


#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);

my $action;

$action = param('action');

if($action eq 'enterrec')
{
print header();

print <<_EOP_;
<HTML>
<HEAD><TITLE>Add Video</TITLE></HEAD>
<body bgcolor="#000000" background="../bkgnd.gif" text="#FF0000" link="#E4B407" alink="#E4B407" vlink="#E4B407">
<p> </p><p> </p><p> </p><p> </p>
<p align="center"><font face="Arial" size="3"><b>Enter Video Details To Add To Database:</b></font></p><p> </p>
<p align="center"><form action="vbase.pl" method="post">
<font face="Arial" size="2"><b>
<input type="hidden" value="addrec" name="action">
Title: <INPUT TYPE="text" NAME="title" SIZE="30"><br><br>
Genre: <INPUT TYPE="text" NAME="genre" SIZE="30"><br><br>
Rating: <INPUT TYPE="text" NAME="rating" SIZE="30"><br><br>
Year: <INPUT TYPE="text" NAME="year" SIZE="30"><br><br>
Director: <INPUT TYPE="text" NAME="director" SIZE="30"><br><br>
Stars:<br><TEXTAREA NAME="stars" ROWS="4" COLS="30"></TEXTAREA><br><br>
Available (Checked = Yes): <INPUT TYPE="checkbox" NAME="av" VALUE="checked"></b></font><br><br>
<INPUT TYPE="image" SRC="../submit.gif" BORDER="0" ALT="Submit!">
</form>
_EOP_

print end_html;
}
elsif($action eq 'addrec')
{
my $title = param('title'); #Sets title parameter to variable title.
my $genre = param('genre'); #Sets genre parameter to variable genre.
my $rating = param('rating'); #Sets rating parameter to variable rating.
my $year = param('year'); #Sets year parameter to variable year.
my $director = param('director'); #Sets director parameter to variable director.
my $stars = param('stars'); #Sets stars parameter to variable stars.
my $av = param('av'); #Sets availability paremeter to variable av.
&addrec($title, $genre, $rating, $year, $director, $stars, $av);
}
elsif($action eq 'delrec')
{
my $title = param('title');
&delrec();
}
elsif($action eq 'search')
{
my $sstring = param('sstring');
&search($sstring);
}
elsif($action eq 'modrec')
{
my $title = param('title');
&modrec();
}
else{

print header();

print <<_EOP_;
<HTML>
<HEAD><TITLE>Search For Video</TITLE></HEAD>
<body bgcolor="#000000" background="../bkgnd.gif" text="#FF0000" link="#E4B407" alink="#E4B407" vlink="#E4B407">
<p> </p><p> </p><p> </p><p> </p>
<p align="center"><font face="Arial" size="3">
<b>Enter Film Title To Search For:</b></font></p>
<p align="center">
<form action="vbase.pl" method="post">
<input type="hidden" value="search" name="action">
<INPUT TYPE="text" NAME="sstring" SIZE="30"><br><br>
<INPUT TYPE="image" SRC="../submit.gif" BORDER="0" ALT="Submit!">
</form>
_EOP_

print end_html;
}


sub addrec{

my $av;
my($title, $genre, $rating, $year, $director, $stars, $avr) = @_;

if($avr eq 'checked'){
$av = 1;
}
else{
$av = 0;
}

if (open(VDB, ">>vbase.db"))
{
printf(VDB "%s\t%s\t%s\t%s\t%s\t%s\t%d\n", $title, $genre, $rating, $year, $director, $stars, $av);
close VDB;

print header();

print <<_EOP_;
<HTML>
<HEAD>
<META HTTP-EQUIV="refresh" content="10;URL=http://www.super8video.f2s.com/cgi-bin/vbase.pl?action=enterrec">
<TITLE>Record Added!</TITLE></HEAD>
<body bgcolor="#000000" background="../bkgnd.gif" text="#FF0000" link="#E4B407" alink="#E4B407" vlink="#E4B407">
<p> </p><p> </p><p> </p><p> </p>
<p align="center"><font face="Arial" size="3">
<b>Film Record Added!</font><br>
<font face="Arial" size="2">Taking You Back To Add A Record Menu In 10 Seconds</b></font>
</p><p> </p>
_EOP_

print end_html;
}
else{

close VDB;

print header();

print <<_EOP_;
<HTML>
<HEAD>
<META HTTP-EQUIV="refresh" content="10;URL=http://www.super8video.f2s.com/cgi-bin/vbase.pl?action=enterrec">
<TITLE>Failed Adding Record</TITLE></HEAD>
<body bgcolor="#000000" background="../bkgnd.gif" text="#FF0000" link="#E4B407" alink="#E4B407" vlink="#E4B407">
<p> </p><p> </p><p> </p><p> </p>
<p align="center"><font face="Arial" size="3"><b>File Opening Failed!</font><br>
<font face="Arial" size="2">Taking You Back To Add A Record Menu In 10 Seconds</b></font></p><p> </p>
_EOP_

print end_html;
}
}

sub search{

my ($line, $sstring, $first, $title, $genre, $rating, $year, $director, $stars, $av);
my @record;

$sstring = $_[0];
$sstring = lc($sstring);
}


I'm still looking for a way to search through my records if anyone can help, hehe, I just need to search the titles. :D
Thanks

[ 23 February 2001: Message edited by: MrNewbie ]

MrNewbie
02-24-2001, 07:20 AM
Nevermind I finally managed to get it working! Woohoo!