Make Homepage | Add To Favorites | Print Page | Submit News | Feedback | Contact | 

Your Technical Computer Information Resource!  

  Weighted Banner Rotating System with ColdFusion  
TACKtech Corp. > Articles > Software > ColdFusion

Weighted Banner Rotating System with ColdFusion (TTID #97)

Author: Travis   Views: 18,447 /  Created: April 29, 2002
Everyone has seen how to do a rotating banner system with ColdFusion or some other language but adding the twist of weighted banners is the part many people either don't know to do or keep quite about so people have to buy there product or script. Well, I will show you how to do this with ColdFusion. It is really a simple script once you see it. Your database needs to be designed like so.

Field Name Type Description
ID Integer Auto incrementing
BANNER_ALT Text Alt description
BANNER_PIC Text Picture location
BANNER_URL Text URL link for banner
BANNER_WEIGHT Integer A weight from 0-10
BAN_CLICKS Number How many times clicked
BAN_VIEWS Number How many times viewed

Assuming you have some banners inputted in to your database, you can move on to the CF coding. First thing you need to do is query your database for the data like so.

<!--- Start Query Banner DB --->
<cfquery datasource="YourDSN" name="banner">
select *
from banners
order by ID desc
<!--- End Query Banner DB --->
Now, it is time to take all your banners and shuffle them and pick the magical banner to display. This next part will be documented fully so you can see how the weighted system is accomplished.

<!--- Start Weighting and Rand Num. --->
<!--- This starts your list for all the numbers to pick from --->
<cfset prodlist="">
<!--- Telling it to output the data your queried --->
<cfoutput query="banner">
<!--- Starting the loop --->
<cfloop index="x" from="1" to="#banner_weight#">
<!--- This added the banner ID to the list for every weight --->
<cfset prodlist=listappend(prodlist,#id#)>
<!--- The end of the looping --->
<!--- End of the data query --->
<!--- This startment pulls a random number from 1 to the length of the list --->
<cfset magicnumb=randrange(1,listlen(prodlist))>
<!--- This one will pull the banner ID out from the random number point --->
<cfset magicnum=listgetat(prodlist,magicnumb)>
<!--- End Weighting and Rand Num. --->
Almost there. The weighting and random part is done now to add one view to the Banner_views variable of the particular banner.

<!--- This queries the specific banner --->
<cfquery datasource="YourDSN" name="views">
select *
from banners
where id = #magicnum#
<!--- start Adds 1 to view --->
<cfoutput query="views">
<CFSET add = #ban_views# + 1>
<CFQUERY datasource="YourDSN" name="UpdateView">
UPDATE Banners Set ban_views = #add# where ID = #id#
<!--- End Adds 1 to view --->
This next part will display the banner.

<cfoutput query="views">
<A HREF="redirect.cfm?ID=#magicnum#" TARGET="_blank">
<IMG SRC="#banner_pic#" alt="#banner_alt#" width="468" height="60"></A>
Ok, now for the final part and that is where the system added one to the Banner_clicks. This will be a different file. It's called redirect.cfm. This is what you will find in the redirect.cfm.

<!--- Query Banners To Get ID to specify Redirection URL --->
FROM Banners

<!--- Add One To Click --->
<CFSET add = #incrementvalue(val(Banners.ban_CLICKS))#>
<CFQUERY datasource="YourDSN" name="UpdateView">
UPDATE banners Set ban_clicks = #add# where ID = #ID#

<!--- Go To URL for that Banner --->
<CFLOCATION URL="http://#Banners.banner_url#">
That is it. You have now just created a weighted banner rotation system. Nice thing that is also added to this is if you set the weight of the banner to 0 it will not display so you can turn banners off. If you have any questions or comments, Contact Me.

Revision History:

02.25.2009 - Adjusted coding. Had unneeded lines.

Your Name:

Your E-Mail: (required)

Friend's E-Mail: (required)

View Our World Wide Web Customer Privacy Policy
  Featured Articles  
  Quick Links  
  Top Affiliates