| Category: | CGI Programming |
| Author/Contact Info | Hero Zzyzzx |
| Description: | Neat little program to create a categorized link list with click-thru tracking. It uses CSS for output formatting. I've only been perl programming for a couple of months, so please go easy on me. I know I need to learn the CGI module, etc., but all things in due time. This works well included as SSI The mySQL table was created with the command below, I added the indeces thinking I might use them down the road.
I'm sure you could tweak the field definitions. Comments? UPDATE, 4/5/01 I fixed the code to run under strict and use CGI.pm for parameter parsing. If I had more time, I'd also fix it to use CGI.pm to output all the HTML, but I don't think it's worth fixing at this point. |
#!/usr/bin/perl -wT
use strict;
use DBI;
use CGI;
my $q= new CGI;
my $dbh;
#Database Connect
$dbh = DBI->connect( "dbi:mysql:dansite","dan","vindaloo") or
dienice("Can't connect: ", $dbh->errstr);
if (!$q->param("action")){
linklist();
}elsif($q->param("action") eq "loadpage"){
loadpage();
}elsif($q->param("action") eq "savepage"){
savepage();
}else{
linklist();
}
$dbh->disconnect();
sub loadpage{
my $ath;
my $linktoprint= substr($ENV{'QUERY_STRING'},24,100);
$ath=$dbh->do("update links set visits=visits+1 where urllink=\"$l
+inktoprint\"")
or dienice($dbh->errstr);
print"Location:$linktoprint\n\n";
}
sub savepage{
my $category= $q->param("category");
$category=~s/\+/ /g;
my $description=$q->param("description");
$description=~s/\+/ /g;
my $sth = $dbh->prepare("insert into links values(?,?,?,?)");
$sth->execute($q->param("urllink"),$category,$description,$q->para
+m("visits"))
or dienice($dbh->errstr);
$sth->finish();
print"Location:/addlink.html\n\n";
}
sub linklist{
my ($urllink,$urlcategory,$description,$visits,$catttest,$tbkgrnd)
+;
print "Content-type:text/html\n\n";
my $sth=$dbh->prepare("select urllink,urlcategory,description,visi
+ts ".
"from links order by urlcategory,description")
or dienice("can't execute query ",$dbh->errmsg);
$sth->execute;
my $cattest = "bb";
print"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" widt
+h=\"100%\">\n";
while(($urllink,$urlcategory,$description,$visits)= $sth->fetchrow
+_array){
if ($urlcategory ne "Our Site"){$tbkgrnd = "#993300"}
else {$tbkgrnd = "#660000"}
if ($urlcategory ne $cattest){
if ($cattest ne "bb"){
print"<tr><td colspan=\"3\"><hr noshade style=\"co
+lor: AAAAAA\">",
"</td></tr>\n";
}
print"<tr>\n<td colspan=\"3\" bgcolor=\"$tbkgrnd\">",
"<font class=\"littleheader\">$urlcategory</font></td>
+</tr>\n";
}
print"<tr>\n<td class=\"linkfont\" bgcolor=\"$tbkgrnd\">"
"<font size=\"1\"> </font></td>\n",
"<td class=\"linkfont\" bgcolor=\"$tbkgrnd\"><font size=\"
+1\">",
"<a href=\"/cgi-bin/link.pl?action=loadpage&urllink=$urlli
+nk\" ",
"target=\"_win\">$description</a>",
"</font></td>\n<td class=\"linkfont\" bgcolor=\"$tbkgrnd\"
+>",
"<font size=\"1\"> $visits</font>",
"</td>\n</tr>\n";
$cattest = $urlcategory;
}
print"<tr>\n<td align=\"right\" class=\"linkfont\" colspan=\"3
+\"><font size=\"1\">",
"<hr noshade style=\"color: AAAAAA\">Total<br>",
"Visits From<br>This Page</font></td>\n</tr>\n",
"</table>\n";
$sth->finish();
}
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: mySQL Based link list, with click-thru tracking
by mirod (Canon) on Feb 20, 2001 at 13:10 UTC | |
|
Re: mySQL Based link list, with click-thru tracking
by Anonymous Monk on Feb 20, 2001 at 22:57 UTC |