Creating a Link Management System

Link Management Tutorial

Recently posted on the EasyCFM Message boards was a request for a total Link management system, first calling just to click a link and it count the link, seemingly quite an easy thing to do, until you start looking at what needs to be done first to get to that piece of code, and then looking at else what you can do.

You can download the files that i created at 
http://webdev.webleicester.co.uk/test/links/links.zip

I won't go through every file, but just give a few pointers as to whats happening across the board, comments in the code are there to help you and the layout is fairly logical, so you should be able to follow without much difficulty


The Database

For this we just have a simple database called links, we have the following table, called Links setup

    Link_id = Autonumber
    Link = Text
    short_desc = Text
    full_desc = Text
    visits = Numeric (Required: YES, Default 0)

If you don't put the Required field and default in is that SQL will return it saying you cant make additions to "", which is basically nothing in the field.


Application.cfm 

First and foremost we have the Application file where we have the setting for the DSN, and a few other settings, its always good to set this up because as a site becomes bigger, if you change the DSN name for whatever reason, you have to change each dsn part in each file

We haven't got any session management or cookies on because for the moment that seems a bit of overkill this would only be enabled if you wanted to track what your members were doing, useful for intranet purposes


Index.cfm

Here we have the Index.cfm, which is the public version that people will be able to see you'll have something like the following

---
EasyCFM
A great Coldfusion Orientated website serving all members of the community
---

Which is basically

---
<a href="#link#">#short_desc#</a><br>
#full_desc#
---

So rather than having

---
http://www.easycfm.com
a great coldfusion...etc
---

We have a short description (short_desc) to make it look more friendly! Each link will however be presented as such

---
<a href="index.cfm?id=#link_id#" target="_blank">#short_desc#</a><br>
#full_desc#
---

So what we're actually doing is linking back to this page, Index.cfm but this time quoting a link_id, the target="_blank" will mean it opens up in a new window.

Now we get to the part where we will actually count the link The following parts have to be at the beginning of the template after you have made your Query to the database for the Link records so first thing we do is just retrieve all the records, and then set the amount of visits it has

---
<cfset visits = #GetLinks.visits#>
---

Then we are going to use the

---
<cfif isdefined("url.id")>
---

This is a good idea basically so we just have the one file and we don't need to create another page to link too, just makes it easier for you to manage,

The code we will then use is this

---
<cfif isdefined("url.id")>
    <cfset visits = visits + 1>

    <cfquery name=
"GetLink2" DATASOURCE="#application.dsn#">
        SELECT link
        FROM links
        WHERE link_id=#url.id#
    </cfquery>

    <cfquery name=
"UpdateLink" Datasource="#application.dsn#"
        UPDATE Links
        SET visits=#visits#
        WHERE link_id=#url.id#
    </cfquery>

    <CFOUTPUT QUERY=
"GetLink2">
        <html>
            <head>
                <meta http-equiv=refresh content=
"0;URL=#link#">
            </head>

            <body>
            <center>

            <table width=700>
                <tr>
                    <td bgcolor=##000000>

                    <font color=##AAAA77 face=verdana,arial size=2>
                    If your browser does not support javascript page redirection, click <a href="#link#">here</a>.
                    </font>
                    </td>
                </tr>
            </table>

            </center>
        </html>

    </CFOUTPUT>
</cfif>

---

So basically what the scripts done, is its asked is there an "?id=x" in the URL? If there is, forward it to that URL if there isn't just forget this part and show the available links. 

The meta refresh tag is by far the easiest way to do a redirect you could use code in Dreamweaver MX and other programs, but they tend to be lots of Javascript that just is too much hassle, but you can do as you wish.

so there we have it, an incredibly easy to use link management system, the rest of the files are just your common inserting into a database

Hope this helps you all!!! :)

If you have any problems, or something doesn't work you can either just message me on the easycfm forums or email me at dearohdear@hotmail.com

All ColdFusion Tutorials By Author: Alex Allen-Turl
  • Creating Relationships with ColdFusion
    This tutorial shows how you create Relationships between tables to speed up queries and decrease the overall size of the database
    Author: Alex Allen-Turl
    Views: 23,415
    Posted Date: Friday, December 13, 2002
  • Creating Databases in MS SQL2000
    How to get your Data into MS SQL2000 and interface with Coldfusion in 34 Easy Steps!
    Author: Alex Allen-Turl
    Views: 27,172
    Posted Date: Sunday, December 15, 2002
  • Creating a Link Management System
    This quick tutorial, shows you how to creat a Link system, that will count the amount of hits that your links create.
    Author: Alex Allen-Turl
    Views: 24,100
    Posted Date: Tuesday, December 17, 2002
  • Creating a Voting System
    This is a very quick tutorial on how to create a voting system for page relavances and other such things from a scale of 1 to 5!
    Author: Alex Allen-Turl
    Views: 26,188
    Posted Date: Saturday, December 21, 2002
  • Creating Dynamic Image Galleries
    A tutorial showing you how you can upload one Full sized picture, and have a thumbnail automatically created for you, along with descriptions of the image!
    Author: Alex Allen-Turl
    Views: 38,979
    Posted Date: Tuesday, December 24, 2002
  • Creating Dynamic Bar Charts
    A tutorial showing you how you can create a dynamic bar chart!
    Author: Alex Allen-Turl
    Views: 27,548
    Posted Date: Tuesday, December 24, 2002
  • The Easiest Way to make a form!
    This is by far the most easiest and simplest way to make a form and update it to the database, very low maintenance and very very quick!!
    Author: Alex Allen-Turl
    Views: 27,854
    Posted Date: Tuesday, December 24, 2002
  • Extensionless Coldfusion
    Ever wanted to be like google and run your scripts without an extension? This tutorial shows you how with Apache or IIS!
    Author: Alex Allen-Turl
    Views: 18,067
    Posted Date: Thursday, October 27, 2005
Download the EasyCFM.COM Browser Toolbar!