By: Chris

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>

About This Tutorial
Author: Chris
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF5
Total Views: 27,990
Submission Date: July 25, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial
  • 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

  • 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

  • I tried this....& it works good...

  • Is your DSN called image? Also you need a > at the end to close the tag.

  • hi sparty.. i got error message said too few paramenter .exptect 1 in list.cfm line 9 and line 11 in default.cfm

  • What is wrong with the tutorial?

  • 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

Advertisement

Sponsored By...
Powered By...