Server Time:
Thursday May 22 2008 02:18 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Dynamic Image Gallery
by: D Evans
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

Dynamic Display Image Gallery

In this tutorial you will be shown how to display the images in the desired number of
columns and desired number of records per page with minimal coding. When the thumbnail image is clicked on it will open another window with the large image and image description under it.

Insert this open window java script into the head of your html file :

<script language="JavaScript">
   <!-- hide from JavaScript-challenged browsers
  function openWindow(url) {
              popupWin = window.open(url, 'remote',                 
                               'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,        
                               dependent,width=575,height=500,left=0,top=0')
   }
   // done hiding -->
</script>



Next you set a few defaults.
Using the Cfparam and cfset tag :

This will set our table columns to 3
<CFPARAM name="columndisplay" default="3">


This will set the number of images to display to 24 making 8 rows in out table
<CFPARAM name="DisplayAmt" default="24">

This will set our table column start number to 0
<CFPARAM name="tc" default="0">


This sets our beginning record number to 1 and will be used in our next records
<CFPARAM name="StartRecord" default="1">


This will set the directory our images are stored
<CFSET dir="gallery/">


Why use cfparam and not cfset or visa versa?
The rule of thumb I use is: 
If somewhere in the cfm page I am going to reset the variable I use the cfparam tag. If the variable will not change again I use the cfset. 

Set up a table in an access databse named gallery, include 4 items:
Image id-autonumber
Sm_image-for our thumbnails-text 
Lg_image-for the normal size image-- text
Image text-(optional)-memo (I used to memo to accommodate large descriptions)

Then we query our database, selecting all fields. You can replace the field names with * if desired


<cfquery name="getdetails" datasource="gallery">
   select sm_image,imgid
   from gallery
</cfquery>


Now your will create a previous & next recordset
Write an if statement that checks if the query recordcount is larger than the DisplayAmt
variable set in the cfparam tag. If There are more records than the display amount set the DisplayNext variable equal to (start record)1+(displayAmt)24-1,
If it is not greater than set the DisplayNext variable to the amount of records--->

<cfoutput>
  <CFIF #Getdetails.RecordCount# GT (StartRecord + (DisplayAmt -1))>
    <CFSET DisplayNext = StartRecord + (DisplayAmt -1)>
  <CFELSE>
    <CFSET DisplayNext = Getdetails.Recordcount>
  </CFIF> 

Write an if statment: if the recordcount is not 0 this will write displaying from record number 
and to record number of the total amount of records on the top of the gallery


<CFIF Getdetails.RecordCount IS NOT 0>
Displaying #StartRecord# to #DisplayNext# of #Getdetails.RecordCount# 
Match<CFIF Getdetails.RecordCount GT 1>es</CFIF> Found...


EX: 
Displaying 1 to 24 of 49 Matches Found...
[NEXT] 


This next if statement will add a previous button if you have more than the DisplayAmt number of records in the gallery and have gone past the first set of records:

<CFIF StartRecord GT 1>
  <CFSET PrevRecord = (#StartRecord# - #DisplayAmt#)>

  <B>[<A HREF="gallery.cfm?StartRecord=#PrevRecord#"> LAST</A>]
</CFIF>
This if statement will add the next link if you have more than the DisplayAmt of records in the recordcount of the query or write the no records found
statement if the query recordcount is 0.

<CFIF Getdetails.RecordCount GT (#StartRecord# + (#DisplayAmt# - 1))>
<CFSET NextRecord = #DisplayNext# + 1>

   <B>[<A HREF="gallery.cfm?StartRecord=#NextRecord#">NEXT</A>]
</CFIF>
<cfelse>


  There are no images at this time in the Gallery.
  Please check back soon.

</CFIF> 
</CFOUTPUT>
This is the end of the previous -next navigation .

Note Here: Usually I keep the above navigation code in a file called prev_next.cfm
And use an include statement instead of all of that coding on the page. Just remember to always name your query get details or you will get an error.



Now to display our images.

<table width="500">
Loop through the query setting start rows and endrows with our #startrow# and #displaynext# variables

<cfloop query="getdetails" startrow="#startrecord#" endrow="#displaynext#">

This if statement checks to see if the tc variable is equal to 0, if it is it writes a <tr>
<cfif tc eq 0><tr></cfif>

Use the columndisplay variable to set the width of the table cells. Since I only use 2 or 3 I used else, but you can write it for as many as you want. 

<td <cfif #columndisplay# is 2 > width="50%"<cfelse>width="33%"</cfif>>

Use the output tag inside the <td> tag to display the images. Using it anywhere else will cause the images to repeat the displayAmt times the query recordcout.


I used the javascript to open a new window containing the lg_image.cfm file
and set a default width on the thumbnail image in the event the image is larger than a thumbnail and added 2 <br> tags for spacing .


<cfoutput> 
<a HREF='javascript:openWindow("lg_img.cfm?imgid=#getdetails.imgid#")'><img src="#dir##getdetails.sm_image#" border=0 width=100 alt=""></a>


I then reset the tc variable to increment by one and closed the cell

<cfset tc=#tc#+1>
</cfoutput>

</td>

Now I check to see if the tc varaible is equal the the columndisplay number 
I set in the cfparam tag. If it is, it will write the ending row tag </tr> and reset the tc variable to 0, if it is not it will repeat the <td> tags again.

<cfif tc eq #columndisplay#>
</tr>
<cfset tc=0> 
</cfif>


Close our Loop and end our table and html document and body tags.

</cfloop> 

</table>

</b></b>


You have now written the Gallery thumbnail display page.
One last page we need is the lg_image.cfm file to display the large images in.
<p>

Set the default dir ( actually I usually set this in the application page and 
then I don't have to include it on every page).

<cfset dir="gallery/">

Write a query to select the lg_img and img_text from the dbase using the 
url.imgid passed in our gallery.cfm page link

<cfquery name="getimg" datasource="gallery">
   SELECT lg_image,img_text FROM gallery
   where imgid = #url.imgid#
</cfquery>

Write the output statement to output our Image and text 
<cfoutput query="getimg">
<img src="#dir##lg_image#">
#img_text#
</cfoutput>
<p>

I have included all of the files needed to run this tutorial in this zip file


Date added: Sun. December 8, 2002
Posted by: D Evans | Views: 23517 | 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:
Make a generic email form processor
This code will process any form submitted and email the submitted form fields excluding the submit, the redirect, the hidden, and the required fields; to the designated recipient. - Date added: Fri. December 13, 2002
Checking for submitted form fields
This sets all of the submitted form fields and values into a list then loops through the list of fields and values. - Date added: Fri. December 13, 2002
Frameless Frames in CF
The appeal of frames and the continued use is -- to leave one constant item on a part of the page. However clever, they are a pain to code, setting targets and defining the columns and then there that scroll bar thing. Here is an easy way to create the illusion of frames on your page. - Date added: Wed. December 11, 2002
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
Close Javascript
I believe you need to add a ; at the end of your function to close that.<br><br>
<font color="#800000"><script language=</font><font color="#0000ff">"JavaScript"</font><font color="#800000">></font><br>
   <font color="#808080"><i><!-- hide from JavaScript-challenged browsers</i></font><br>

  <font color="#000080"><b>function openWindow(url)</b></font> <font color="#000080"> {</font><br>
             
popupWin = window.open(url,
'remote',                 <br>
                              
'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,        <br>
                              
dependent,width=575,height=500,left=0,top=0')<b>;</b><br>
   <font color="#000080">}</font><br>

<font color="#808080"><i>   // done hiding --></i></font><br>
<font color="#800000"></script></font><br>


Posted by: Sean
Posted on: 12/11/2004 02:41 PM
cant get it work ....
Dear staff ..

i had extract the zip file to wwwroot\gallery and set my datasource name gallery and in the application.cfm , i changed the <cfset dir="gallery/"> to <cfset dir="c:\cfusion mx\wwwroot\gallery\">,then i load the gallery.cfm , it show message that page cannot be display ? is there anything wrong ?
my datasource name is "gallery"
sincerely yours

cfgreenhorn
Posted by: cfgreenhorn
Posted on: 01/10/2005 12:50 AM
auto fix to image size
I have implemented this tutorial, and works fine. But I have a question how to make that popupWin resize automaticly fit to the image and some text (description) about the image. Please help
Posted by: baong
Posted on: 02/05/2006 10:37 PM
members only feature?
This is a great one - I was wondering how this could be adapted to displaying only certain images to the general public, but all images to logged on users?

Thanks
Posted by: Louis
Posted on: 05/11/2007 04:22 PM
response to Members Only Feature
Louis,

You could rename the files in the directory with a prefix like pri_<imagename.gif>. On your output, you would filter out pri_ if the user was not logged in or include pri_ if they were logged in.
Posted by: JOhn
Posted on: 06/28/2007 11:49 AM
CONTADOR DE STRING
HOLA A TODOS

NECESITO UN CONTADOR DE UNA CADENA DE CARACTERES PARA PODER PASAR A UN ARRAY.

GRACIAS
Posted by: hola
Posted on: 08/28/2007 10:44 AM
Large Image window problem
Hi,
First let me say thank you very much for the tutorial. I was just what I was looking for.

I got it assembled and it looks great. The only problem is that it works perfectly in Firefox but I can't get the pop open window with the large files to work in IE. I need it to work in both

Any suggestions?


Posted by: Elsinore
Posted on: 04/07/2008 06:29 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
ColdFusion Hosting by HostMySite

You are 1 of 365 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved (Server: www0001)
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