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