SoFunction
Updated on 2025-04-14

Coldfusion MX advertising rotation system production tutorial

wait blue ideal
CF does not provide advertising components like those in ASP, but this does not mean that it cannot create a powerful advertising system. Here I put a simple advertising rotation system, which is also used in my CCF forum. It has the number of displays and clicks. It is easier to expand on this basis and can be displayed in a classified manner. The code below only shows a large advertising bar. Just want to draw a brick and draw inferences, and you can create a more powerful advertising system by learning from it.

<!--- Judge whether there is address transmission --->
<CFIF NOT IsDefined("")>
<!---  Default is to display ads --->
<CFLOCK TIMEOUT="10"> 

<CFPARAM NAME="" TYPE="string" DEFAULT="">

<CFIF ListLen() EQ 0>
<cfquery name="getID" datasource="#DSN#">
select adid from ad
</cfquery>

<CFSET  = ValueList()>
</CFIF>

<CFSET ThisAdID = ListGetAt(, 1)>
<CFSET  = ListDeleteAt(, 1)>
</CFLOCK> 

<cfquery name="ad" datasource="#DSN#">
select * from ad
where adid='#ThisAdID#'
</cfquery> 

<!---   Update display times --->

<CFSET ADSHOW=+1> 

<cfquery name="updateshow" datasource="#DSN#">
UPDATE AD
SET show='#ADSHOW#'
where ADID='##'
</cfquery> 

<!--- Show ads --->
<a href="javascript:()">Refresh</a><p>
<cfoutput query="ad">
<a href="?ADID=#URLEncodedFormat(ADID)#" title="#title#" target="_blank"><img src="#IMAGE#" border="0" alt="#title#"></a><p> 

<br>
</cfoutput> 

<cfelse> 

<!--- If there is a parameter passed --->
<cfquery name="gotoURL" datasource="#DSN#">
SELECT ADID,URL,CLICK FROM AD
WHERE ADID='##'
</cfquery> 

<!--- Add 1 -->
<CFSET ADCLICK=+1> 

<CFQUERY DATASOURCE="#DSN#" name="UPDATECLICK">
UPDATE AD
SET CLICK='#ADCLICK#'
WHERE ADID='##'
</CFQUERY> 

<!--- Turn to advertising link address --->
<cfoutput query="gotoURL">
<SCRIPT LANGUAGE="JavaScript"> 
 ='#URL#'; 
</SCRIPT> 
</cfoutput>
</CFIF>



Code copy box
The following is a quoted snippet:

<!--- Judge whether there is address transmission --->
<CFIF NOT IsDefined("")>
<!---  Default is to display ads --->
<CFLOCK TIMEOUT="10"> 

<CFPARAM NAME="" TYPE="string" DEFAULT="">

<CFIF ListLen() EQ 0>
<cfquery name="getID" datasource="#DSN#">
select adid from ad
</cfquery>

<CFSET  = ValueList()>
</CFIF>

<CFSET ThisAdID = ListGetAt(, 1)>
<CFSET  = ListDeleteAt(, 1)>
</CFLOCK> 

<cfquery name="ad" datasource="#DSN#">
select * from ad
where adid='#ThisAdID#'
</cfquery> 

<!---   Update display times --->

<CFSET ADSHOW=+1> 

<cfquery name="updateshow" datasource="#DSN#">
UPDATE AD
SET show='#ADSHOW#'
where ADID='##'
</cfquery> 

<!--- Show ads --->
<a href="javascript:()">Refresh</a><p>
<cfoutput query="ad">
<a href="?ADID=#URLEncodedFormat(ADID)#" title="#title#" target="_blank"><img src="#IMAGE#" border="0" alt="#title#"></a><p> 

<br>
</cfoutput> 

<cfelse> 

<!--- If there is a parameter passed --->
<cfquery name="gotoURL" datasource="#DSN#">
SELECT ADID,URL,CLICK FROM AD
WHERE ADID='##'
</cfquery> 

<!--- Add 1 -->
<CFSET ADCLICK=+1> 

<CFQUERY DATASOURCE="#DSN#" name="UPDATECLICK">
UPDATE AD
SET CLICK='#ADCLICK#'
WHERE ADID='##'
</CFQUERY> 

<!--- Turn to advertising link address --->
<cfoutput query="gotoURL">
<SCRIPT LANGUAGE="JavaScript"> 
 ='#URL#'; 
</SCRIPT> 
</cfoutput>
</CFIF>



GAME OVER
This advertisement was modified from the tutorial of BEN FORTA. The principles of the advertising strip production methods he introduced were similar, but the functions were pitiful and he could not turn URLs. He could not record the number of displays and clicks, so I changed it and added some functions to it myself.


Principle: Put the advertising data in a database table, extract all the ad numbers (IDs) at once, store the ID in an APPLICATION variable. I display one, and delete the displayed ad ID from this APPLICATION variable. The ad bar will not be displayed next time. Until all the ad bars are displayed, if the APPLICATION variable is empty, re-query the database and retrieve all ad IDs, and keep looping like this.


Tip: Change the file saved above to your own database line. The ads are displayed and the URLs of the ad strip are all in this file.

Please note this variable:  , This is an application variable, I define it as a string, which can be used in the CF string ="I'm wait" =" 1 2 3 4 5 6 7 9". I use the following ad
Let’s talk about this metaphor: For example, it’s my first visit. I call ="1" and delete this ="1" next time, there will be only: =" 2 3 4 5 6 7 9” one less at a time, and this achieves the rotation effect. Of course, until all of them are displayed, then re-query the database and extract all ad IDs. Put them in this string
Pay attention to this sentence:
<CFIF ListLen() EQ 0> 

Meaning: If my length is 0, I will re-query the database and put the query result in the variable. Note. To test the length of the list, you cannot use LEN(), but use a function ListLen that specifically tests the length of the list.
<cfquery name="getID" datasource="#DSN#">
select adid from ad
</cfquery> 

<CFSET  = ValueList()> 

If there is still no rotation displayed, it is definitely not equal to 0, and the database will not be requeried.

As for getting the first character, you can use it
<CFSET ThisAdID = ListGetAt(, 1)>
Then delete this. Next time it's the next turn.
<CFSET  = ListDeleteAt(, 1)> 

The key point is this. As for the update display times and count display times below, they are all ordinary CF codes. There is nothing special.