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

Using Arrays in ColdFusion To Properly Display Data....
by: Pablo Varando
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

This tutorial will show you how to use arrays to properly display your database information

This tutorial will show you how to use arrays to properly display your database information. In this example we will be creating a image gallery. It will populate a "10" item array and display the data in two rows with 5 items site by side.

The first thing you must do is to query your database for all the gallery items. 
NOTE: I used [maxrows="10"] in the query, but you will want to remove it when it goes live and integrate the previous/next tutorial.

<!--- get all your images from the database --->
<cfquery name="qGetGallery" datasource="MyDSN" maxrows="10">
   SELECT ImageTitle, ImagePath
   FROM Gallery
   ORDER BY ImageType
</cfquery>

Next you simply create the array that will hold the data from the database.

<!--- Create an array of all gallery items --->
<cfset gallery = ArrayNew(2)>

Now here's a trick I've learned the hard way.. start by populating all ten arrays with a value of "0". Later on we will check to see if the current array has a value of "0". If it does we will know that there is no value and simply not display it.

<!--- Now populate the array with all "0" --->
<!--- this is to catch empty arrays later --->

<cfloop from="1" to="10" index="idx">
   <cfset gallery[idx][1] = "0">
   <cfset gallery[idx][2] =
"0">
</cfloop>

Next, let's populate the arrays with the actual data from the database.

<!--- Now let's populate the array with data --->
<cfoutput query="qGetGallery">
   <cfset gallery[currentrow][1] =
"#qGetGallery.ImageTitle#">
   <cfset gallery[currentrow][2] =
"#qGetGallery.ImagePath#">
</cfoutput>

Here we will actually display the data to the end-user and we will check to make sure that the arrays do not have a value of "0"

<!--- Now display the array values --->
<cfif gallery[1][1] neq "0">
   <!--- Display the gallery items --->
   <cfoutput>
   <table width="600" align="center" border="0">
     <tr>
         <td width=
"20%" align="center">
         <!--- Displays the first gallery item --->
         <img src="#gallery[1][2]#" border="0" alt=" Image "><BR>
         #gallery[1][1]#
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the second gallery item --->
         <cfif gallery[2][1] neq 0>
             <img src="#gallery[2][1]#" border="0" alt=" Image "><BR>
             #gallery[2][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the third gallery item --->
         <cfif gallery[3][1] neq 0>
            <img src="#gallery[3][1]#" border="0" alt=" Image "><BR>
            #gallery[3][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the fourth gallery item --->
         <cfif gallery[4][1] neq 0>
             <img src="#gallery[4][1]#" border="0" alt=" Image "><BR>
             #gallery[4][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the fifth gallery item --->
         <cfif gallery[5][1] neq 0>
             <img src="#gallery[5][1]#" border="0" alt=" Image "><BR>
             #gallery[5][1]#
         </cfif>
         </td>
     </tr>
     <tr>
         <td width=
"20%" align="center">
         <!--- Displays the sixth gallery item --->
         <cfif gallery[6][1] neq 0>
              <img src="#gallery[6][1]#" border="0" alt=" Image "><BR>
              #gallery[6][1]#
         </cfif>
         </td>
         <td width="20%" align="center">
         <!--- Displays the sevent gallery item --->
         <cfif gallery[7][1] neq 0>
             <img src="#gallery[7][1]#" border="0" alt=" Image "><BR>
             #gallery[7][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the eight gallery item --->
         <cfif gallery[8][1] neq 0>
            <img src="#gallery[8][1]#" border="0" alt=" Image "><BR>
            #gallery[8][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the ninth gallery item --->
         <cfif gallery[9][1] neq 0>
             <img src="#gallery[9][1]#" border="0" alt=" Image "><BR>
             #gallery[9][1]#
         </cfif>
         </td>
         <td width=
"20%" align="center">
         <!--- Displays the tenth gallery item --->
         <cfif gallery[10][1] neq 0>
             <img src="#gallery[10][1]#" border="0" alt=" Image "><BR>
             #gallery[10][1]#
         </cfif>
         </td>
     </tr>
  </table>

  </cfoutput>
<cfelse>
  <!--- There is no data to display, no results found --->
  <font size="2" face="Verdana" color="red">We're sorry, no items were found!</font>
</cfif>

Questions? Comments? Email Me......


Date added: Mon. October 28, 2002
Posted by: Pablo Varando | Views: 23767 | Tested Platforms: CF5 | Difficulty: Intermediate
Categories Listed: Charting Functions Working w/Data

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:
Delete files and folders in a specified path!
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows! - Date added: Wed. September 7, 2005
Dynamic Last Date Modified?
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically. - Date added: Mon. April 12, 2004
Correct Content (document) serving!
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit! - Date added: Tue. February 10, 2004
Creating your very own RSS XML Feeds with ColdFusion MX!
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data! - Date added: Thu. January 15, 2004
Processing XML/RSS feeds with ColdFusion MX
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records! - Date added: Sat. December 27, 2003

Additional Tutorials:
· Changing the form submission page on the fly!

· What is the ID for the record I just inserted?

· Creating a file content crawler with ColdFusion....

· Delete Records From Your Database With ColdFusion!

· Do you want to remember your members?

· Get A Folder Size Using ColdFusion and FSO...

· Preventing People From Leeching Your Images!

· Combining two queries into one..

· CaSe SensitiVe password logins!

· Creating an ODBC Connection within ColdFusion MX Server...

· Print your web pages on the fly!

· Using <CFPOP> and creating an email client for POP3 Email Reading!

· Using CFRegistry to Add Your IP To CF Debug IP List!

· Reading your IIS Log Files with ColdFusion!

· Automatically Adding Smiles To Your Messages!

· Implementing FORM Error Checking On Your Pages!

· Inserting FORM data into multiple database tables!

· Creating, Altering and Deleting database tables with ColdFusion.

· Sending multiple attachments with CFMAIL!

· ColdFusion and .INI Files!

· Clearing your session variables!

· Using PayPal's IPN with ColdFusion!

· Alternating Row Colors!

· Previous / Next n Records

· Using Query String Values....

· A quick intro into the world of Custom Tags!

· A brief demonstration of Fusebox 2.0

· Creating a Newsletter System....

· Count Active Users On Your Site.

· User Defined Functions....

· Creating a user athentication (Login) area.

· DSNLess Coldfusion?

· A Simple Contact Us Page….

· Having Your Database Do The Work… not ColdFusion!

· Retrieving Records From a Database..

· Inserting data into a database
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
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 646 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