One of the common user friendly functionality is the ability to updateand/or delete multiple records at the same time. This has benefits forthe user because it is much easier to maintain data and the userdoesn?t need to click hundreds of times to do the same with 1 click.But also the traffic of your database and web server goes downenormously and in the end it will benefit your application. So if youwant to make your customer happy, this is the way to go if you need toupdate records frequently.
In this article we?re going to build a "restaurant menu manager". Withthis manager you can insert dishes, put them in a specific dish typeand set the display order of the individual dishes within a dish type.
Sounds complicated huh?! If you?re not familiar with Coldfusion, it could be, if you are familiar, it isn?t.
So take a look at the prerequisites
Requirements & Basic Setup
Knowledge of using queries, loops, forms and basic usage of CFC?s
Coldfusion MX or Coldfusion MX 7 (I use Coldfusion MX 7 in this article)
Dreamweaver MX 2004 (or any other code editor you prefer)
MS Access 2000 (to view the changes or edit data manually, not necessary)
Place a map called "cfmultiple" in the root of your webserver (i.e. C:Inetpubwwwrootcfmultiple)
Expand the zip in the "cfmultiple" folder
Setting up the Coldfusion Part
Login to your Coldfusion administrator and create a new "Access datasource"
(i.e. http://localhost/cfide/administrator? > Data Sources)
Name the DSN "mainmenu"
Select the "restaurant.mdb" database file in the "db" folder ({webroot}/CFFLASH/db)
Close the administrator??
Database
The database is very simple. It contains 2 tables, 1 table withdishes(tbl_Dish) and 1 with dish types (tbl_DishType). Every dish has adish type ID and a order number. The dish type table also contains aorder number, so you can also display the dish types in a certain way(i.e. soups after starters). So this is pretty straight forward. Theonly hard part is that we let Coldfusion do all the validation checkingand not the database (we?re Coldfusion developers, so we useColdfusion! Maybe a DB administrator can show us how this can be donewith a database).
Coding the Application.cfm
Let?s start with coding the Application.cfm. Open the Application_1.cfm in Dreamweaver and put in the following lines of code.
<cfset request.dsn="mainmenu" />
<cfif IsDefined("URL.action") AND URL.Action EQ "menu" AND IsDefined("FORM.fieldnames")>
????? <cfinvoke component="cfmultiple.manager" method="update">
??????????? <cfinvokeargument name="formfields" value="#FORM#">
????? </cfinvoke>
????? <cflocation url="#cgi.SCRIPT_NAME#"/>
</cfif>
<cfquery datasource="#request.dsn#" name="qGetMenu">
????? SELECT tbl_Dish.DishID, tbl_Dish.DishTypeIDfk, tbl_Dish.DishTitle,
?????? tbl_Dish.DishText, tbl_Dish.DishPrice, tbl_Dish.DishOrder,
?tbl_DishType.DishTypeID, tbl_DishType.DishTypeName,
???????????? tbl_DishType.DishTypeOrder
????? FROM? tbl_Dish, tbl_DishType
????? WHERE tbl_Dish.DishTypeIDfk=tbl_DishType.DishTypeID
????? ORDER BY tbl_DishType.DishTypeOrder ASC, tbl_Dish.DishOrder ASC
</cfquery>
<cfquery datasource="#request.dsn#" name="qGetTypes">
????? SELECT tbl_DishType.DishTypeID, tbl_DishType.DishTypeName,
?tbl_DishType.DishTypeOrder
????? FROM? tbl_DishType
????? ORDER BY tbl_DishType.DishTypeOrder ASC
</cfquery>
I?m not going deep into the first line of code, but the idea of this isthat you don?t need to change the DSN in multiple files if the name ordatabase changes. This can now be done by changing 1 variable in theApplication.cfm. This is very handy if you use a different DSN on yourdevelopment server/pc/laptop and a other one on the production server.
The cfif statement is a little bit overdone, but I like to be sure thatthe invoke of the CFC is only done when the form is submitted. In theform we will submit it to the same page, but with a url parameter. Sothe long cfif is just a double check.
The cfinvoke passes all the form variables by putting them in theargument of the invoke. This input is required, so if no form data ispassed (it will first of all not be run because of the cfif statement)the CFC will throw an error. After we build the form we?ll look intothe CFC. After the invoke is done, we do a cflocation so that the urlvariable is removed and that a refresh cannot try to update and deletethe same records again.
Read More