<!--- Update or delete from database as needed ---> <cfinclude template="update.cfm"> <cfinclude template="delete.cfm"> <!--- Fill and display the list and controls ---> <cfquery name="ItemList" datasource="context"> select * from ItemPrice where Current = 1 order by Item </cfquery> <a href="change.cfm?Mode=Add">Add</a> <table border="1"> <tr><td>Edit</td><td>Item</td><td>Price</td> <td>Delete</td></tr> <cfoutput query="ItemList"> <tr><td> <a href="change.cfm?Mode=Edit&Item=#urlEncodedFormat(Item)#"> Edit</a></td> <td>#Item#</td><td>#Price#</td> <td> <a href="list.cfm?Mode=Delete&Item=#urlEncodedFormat(Item)#"> Delete</a></td></tr> </cfoutput> </table>
<cfparam name="Mode" default=""> <cfif Mode is "Delete"> <cfquery name="deleteItem" datasource="context"> delete * from ItemPrice where Item = '#urlDecode(url.Item)#' </cfquery> </cfif>
<!--- Go back to list if asked ---> <cfparam Name="Back" default=""> <cfif len(trim(Back))> <cfinclude template="list.cfm"> <cfabort> </cfif> <!--- Set defaults ---> <cfparam Name="Mode" default="Add"> <cfparam Name="Go" default=""> <!--- Add to database if requested ---> <cfinclude template="add.cfm">
Fill the form with the real current row if editing; otherwise, use an empty row. Set the form action to the list if editing (so it will return to the list after the change); otherwise, set the form action to this page. You don't want users editing the time directly; so, it's displayed as a label. You want to know if the user has changed the actual item name; so, keep track of the original name (OldItem) in a hidden field for later comparison. The normal submit button (Go) does double duty. Not only will it have an appropriate name (Edit or Add), but the presence of its value is used to tell the page that a new row must be added. This form has two submit buttons. If the Back button (labeled List) is pressed, earlier code will direct the user to the list.
<!--- Fill and display the form appropriately ---> <cfif Mode is "Edit"> <cfquery name="ItemGet" datasource="context"> select * from ItemPrice where Item = '#Item#' and Current = 1 </cfquery> <cfset MyAction="list.cfm"> <cfelse> <!--- Mode is assumed to be "Add" ---> <cfquery name="ItemGet" datasource="context"> select * from ItemPrice where 1 = 0 </cfquery> <cfset dummy=queryAddRow(ItemGet)> <cfset MyAction="change.cfm"> </cfif> <cfoutput> <form name="Change" action="#MyAction#" method="post"> Item: <input name="Item" type="text" maxlength="50" size="55" value="#ItemGet.Item#"><br> Price as of #dateFormat(ItemGet.AsOf)# #timeFormat(ItemGet.AsOf)#: <input name="Price" type="text" maxLength="10" size="12" value="#ItemGet.Price#"> <input name="OldItem" type="hidden" value="#ItemGet.Item#"> <input name="Go" type="submit" value="#Mode#"> <input name="Back" type="submit" value="List"> </form> </cfoutput>
If the item being edited has a history (non-current rows), show the history below the form.
<cfif Mode is "Edit">
<cfquery name="ItemOldList" datasource="context">
select * from ItemPrice
where Item = '#Item#'
and Current <> 1
</cfquery>
<cfif ItemOldList.recordcount>
<table border="1"><tr><td>ITEM</td>
<td>PRICE</td><td>LAST CHANGED</td></tr>
<cfoutput query="ItemOldList">
<tr><td>#ItemOldList.Item#</td>
<td>#ItemOldList.Price#</td>
<td>#dateFormat(ItemOldList.AsOf)#
#timeFormat(ItemOldList.AsOf)#</td></tr>
</cfoutput>
</table>
</cfif>
</cfif>
<cfif Go is "Add">
<cfquery name="ItemGet" datasource="context">
select * from ItemPrice
where Item = '#form.Item#'
</cfquery>
<cfif ItemGet.recordCount>
The item already exists in the database;
no action was taken.
<cfelse>
<cfquery name="ItemAdd" datasource="context">
insert into ItemPrice
(Item, Price, Current, AsOf)
values
('#form.Item#', #form.Price#, 1, #createODBCDateTime(now())#)
</cfquery>
<cfoutput>#form.Item#</cfoutput>
has been sent to the database
</cfif>
</cfif>
<cfparam name="Go" default="">
<cfif Go is "Edit">
<!--- Globally change item name if different --->
<cfif form.Item is not form.OldItem>
<cfquery name="NameChange" datasource="context">
update ItemPrice set
Item = '#form.Item#'
where Item = '#form.OldItem#'
</cfquery>
</cfif>
<!--- Globally mark item not current --->
<cfquery name="ItemAge" datasource="context">
update ItemPrice set
Current = 0
where Item = '#form.Item#'
and Current = 1
</cfquery>
<!--- Add new row for item and mark it current --->
<cfquery name="ItemReplace" datasource="context">
insert into ItemPrice
(Item, Price, Current, AsOf)
values
('#form.Item#', #form.Price#, 1, #createODBCDateTime(now())#)
</cfquery>
</cfif>