Server Time:
Monday May 12 2008 06:55 AM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Random Image
by: Chris
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

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>


Date added: Fri. July 25, 2003
Posted by: Chris | Views: 10196 | Tested Platforms: CF5 | Difficulty: Intermediate
Categories Listed: Full Applications

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Deleting Session When User Leaves Your Page
This tutorial will show you how to delete sessions when a user closes the browser. - Date added: Tue. July 18, 2006
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 - Date added: Sun. January 8, 2006
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
please fix this one..
hi..cj..
i always enjoy your tutorial...even now..
but i do found above code not really work please correct them with all possible... if got time..:(

thanks you

arthur
Posted by: arthur
Posted on: 01/05/2005 02:18 AM
What's wrong?
What is wrong with the tutorial?
Posted by: sparty2809
Posted on: 01/05/2005 06:33 AM
thanks for reply
hi sparty..

i got error message said too few paramenter .exptect 1
in list.cfm line 9 and line 11 in default.cfm <cfquery name = "qGetImages" datasource="image"

don't know what can i do...about it :(


Posted by: arthur
Posted on: 01/06/2005 12:38 AM
DSN
Is your DSN called image? Also you need a > at the end to close the tag.
Posted by: sparty2809
Posted on: 01/06/2005 06:32 AM
This works great....
I tried this....& it works good...
Posted by: Vamsidar Reddy Malgireddy
Posted on: 01/28/2005 04:14 PM
i have some quries
Can you tell me how to create below generated image with random text to user. As below you had shown can you send me document code which i am using coldfusion 5.0

Regards,
Vivek
Posted by: vivek nanda
Posted on: 03/09/2006 04:50 AM
Different images for each table cell
now how would i modify this code for three table cells to bring up different images for each cell? from the database
and select one category of images for each page

Table categories in db
Illustrations
Photos
Paintings
Posted by: Bruce James
Posted on: 04/02/2006 12:58 AM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
Daily Razor - ColdFusion Hosting

You are 1 of 664 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb