Random Image

First off create a database called Images and 3 fields called

image_id (Autonumber)
links (text)
picture (text)

Then do the following:

Default.cfm
<!--- get the images and stroe into memory --->
<cfquery name="qGetImages" datasource="Image">
    SELECT picture, descrip, link
    FROM images
    ORDER BY image_id
</cfquery>

<!--- catch queries with no values --->
<cfif qGetImages.RecordCount>
    <!--- pick a random image from the query --->
    <cfset current_image = RandRange(1, qGetImages.RecordCount)>
    <cfoutput>

    <table>
      <tr>
         <td><a href=
"#qGetImages.link[current_image]#" target="_self"><img src="http://localhost/Images/#qGetImages.picture[current_image]#" border="0" alt=" Image "></a></td>
      </tr>
      <tr>
         <td><a href=
"#qGetImages.link[current_image]#">#qGetImages.descrip[current_image]#</a></td>
      </tr>
    </table>

    </cfoutput>
<cfelse>

    <!--- no images found, display the default image instead --->
    <img src="default.gif" border="0" alt=" Default Image ">
</cfif>

delete.cfm
<cfquery name="qDelete" datasource="Image">
    DELETE picture, descrip, link
    FROM images
    WHERE image_id = #id#
</cfquery>

form.cfm
<form action="upload_action.cfm" method="POST" enctype="multipart/form-data" name="contact" onsubmit='return checkForm();'>
<div>
 <TABLE BORDER="0" bgcolor="#ffffff">
   <TR>
     <TD>
Browse for Image File:</TD>
     <TD>
<input name="picture" size="53" type="file" value=""></TD>
   <tr>
     <td>
Type in Description:</TD>
     <TD>
<textarea name="descrip" rows="7" cols="50"></textarea></TD>
   <TR>
     <TD>
Type in hyperlink:</TD>
     <TD>
<input type="text" name="link" size="66"></TD>
   </TR>
   <TR>
     <TD COLSPAN=
"2"><input type="Submit" value="Insert"></td>
   </tr>
  </TABLE>

</div>
</form>
<BR><BR>
<cfinclude template ="list.cfm">

list.cfm
<cfquery name="qGetImages" datasource="Image">
    SELECT picture, descrip, link, image_id
    FROM images
    ORDER BY image_id
</cfquery>

<!--- Now display the records to the user --->
<HR>
<cfoutput query="qGetImages">
<table>
    <tr><td>
<img src="http://localhost/Images/#qGetImages.picture#" border="0" alt=" Image "></td></tr>
    <tr><td>
#picture#</td></tr>
    <tr><td>
#descrip#</td></tr>
    <tr><td>>
#link#</td></tr>
</table>


<a href="delete.cfm?id=#image_id#" onClick="return confirm('Are You Sure You Want To Delete This Record?');">DELETE</a>

<HR>
</cfoutput>

upload_action.cfm
<cfif picture NEQ ''>
    <CFFILE ACTION=
"upload"
                FILEFIELD=
"picture"
                DESTINATION=
"E:\webserver\Images\"
                NAMECONFLICT=
"MAKEUNIQUE">
    <cfset upload= #file.serverfile#>
    <cfoutput>

        #serverFile# successfully added. Size of the uploaded file:
        #Numberformat(Evaluate(File.FileSize/1024))# KB.</cfoutput>
</cfif>

<br>

<cfquery name="InsertDetails" datasource="Image">
    INSERT INTO Images (picture, descrip, link)
    VALUES ('#listLast(file.serverFile, '\')#', '#form.descrip#', '#form.link#')
</cfquery>

All ColdFusion Tutorials By Author: Chris
  • Customer Complaint System
    this is just a customer complaint tracking system i put together with coldfusion. i got alot of help from easycfm.com so i figured i would get back with an app. its using an access database to store usernames and passwords. did all of the login authentication code myself because my damn server behaviors were not taking. copy the dbase somewhere and use the HCCTS datasource name enjoy
    Author: chris mcintosh
    Views: 19,354
    Posted Date: Sunday, March 23, 2003
  • Creating a Chat System in ColdFusion without using a database!
    Have you ever wanted to have your own chat room? This tutorial will help you have one, but the best thing about this tutorial is that you will not need to use any type of database!
    Author: Chris A.
    Views: 32,713
    Posted Date: Saturday, May 10, 2003
  • Random Image
    Here is a random image display. You can even upload it to the server. Uses a database.
    Author: Chris
    Views: 19,720
    Posted Date: Friday, July 25, 2003
  • Custom Tag - Card Expiration Date
    This tutorial lets you save time when you need dates for credit card processing.
    Author: Chris
    Views: 9,616
    Posted Date: Wednesday, September 8, 2004
  • Random Banner
    This will show you how to create a banner rotation app. It will track the # of views and clicks. It can display flash or image files.
    Author: Chris
    Views: 13,192
    Posted Date: Friday, December 31, 2004
  • Intro to Amazon web services
    Learn the basics of building your own eCommerce site using Amazon.coms web services and Cold Fusion.
    Author: Chris Gomez
    Views: 16,220
    Posted Date: Saturday, December 3, 2005
  • Creating a User Login Area Using a CFC
    This tutorial will show you how to create a user login area using a CFC. This is just a basic idea which can easily be modified to fit your needs
    Author: Chris
    Views: 16,757
    Posted Date: Sunday, January 8, 2006
  • Quick and Dirty Contact Forms.
    This is just a simple way to dynamicly generate contact forms without the need for a database.
    Author: Chris Bunting
    Views: 14,229
    Posted Date: Saturday, April 1, 2006
  • Deleting Session When User Leaves Your Page
    This tutorial will show you how to delete sessions when a user closes the browser.
    Author: Chris
    Views: 12,104
    Posted Date: Tuesday, July 18, 2006
Download the EasyCFM.COM Browser Toolbar!