Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

mySQL Based link list, with click-thru tracking

by Hero Zzyzzx (Curate)
on Feb 20, 2001 at 04:46 UTC ( #59548=sourcecode: print w/replies, xml ) Need Help??
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.
create table links (urllink varchar(120) not null, index urllink (urllink), urlcategory char(120) not null, index urlcategory (urlcategory), description tinytext not null, visits smallint unsigned not null);

I'm sure you could tweak the field definitions.


UPDATE, 4/5/01

I fixed the code to run under strict and use for parameter parsing. If I had more time, I'd also fix it to use 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")){
}elsif($q->param("action") eq "loadpage"){
}elsif($q->param("action") eq "savepage"){

sub loadpage{
    my $ath;
    my $linktoprint= substr($ENV{'QUERY_STRING'},24,100);
    $ath=$dbh->do("update links set visits=visits+1 where urllink=\"$l
    or dienice($dbh->errstr);

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(?,?,?,?)");
    or dienice($dbh->errstr);

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);
    my $cattest = "bb";
    print"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" widt
    while(($urllink,$urlcategory,$description,$visits)= $sth->fetchrow
        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\">",
            print"<tr>\n<td colspan=\"3\" bgcolor=\"$tbkgrnd\">",
                "<font class=\"littleheader\">$urlcategory</font></td>
        print"<tr>\n<td class=\"linkfont\"  bgcolor=\"$tbkgrnd\">"
            "<font size=\"1\">&nbsp;&nbsp;</font></td>\n",
            "<td class=\"linkfont\" bgcolor=\"$tbkgrnd\"><font size=\"
            "<a href=\"/cgi-bin/$urlli
+nk\" ",
            "</font></td>\n<td class=\"linkfont\" bgcolor=\"$tbkgrnd\"
            "<font size=\"1\">&nbsp;&nbsp;$visits</font>",
        $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",
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

    OK, so a couple of generic comments:

    • first this section is meant for complete code, not code where some sub-routines (parseget and dieniceare missing.
    • I know I need to learn the CGI module, etc., but all things in due time.
      _Now_ is the time. Actually before you wrote this program would have been the time. It's not like is hard to grasp: add a use CGI pw(:cgi-lib); at the top of your code, then my %FORM= Vars; instead of your call to parseget and your code will run unchanged, except that the parsing is most likely way more robust than what you have right now. It takes about 10 minutes of scanning the doc to find it, how long did it take you to write parseget?
    • Your indentation style is highly confusing, in your main loop the block under <code>if ($urlcategory ne $cattest){ is not indented, this is the kind of code that makes Python evangelists happy ;--(
    • I see you use -wT which is good, but you forgot their friend use strict;. I would recommend you use strict for anything that's not a one-liner. It will catch typos in variable names for examples.
Re: mySQL Based link list, with click-thru tracking
by Anonymous Monk on Feb 20, 2001 at 22:57 UTC
    Oops. I got a little excited with my first post *blush*. Thanks for the feed back. I'll clean up my indenting and only post complete code in the future.
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://59548]
[msh210]: doesn't append anything.
[msh210]: It should say "If the last nonempty element of LIST" or "If the string thus to be printed does not end in a newline""
[msh210]: Am I wrong?
[shmem]: no. You're right.
[msh210]: About time... last time I was right was... I don't even remember when.
[shmem]: perl -e 'warn "test$/", ^@^ '
[Eily]: well, at least it's right about warn doing the same thing as die :)
[shmem]: same with undef as last element
[msh210]: oh, good point. It's the best kind of correct
[shmem]: msh210: but I'd not call it a lie outright. That's when you do know the truth.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (19)
As of 2017-04-24 12:41 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (439 votes). Check out past polls.