All In One Pages A Technique For Better Code
Posted on 13 Jan 2000
by Mike Roberto (MjR)
Rated 3.74 (Ratings: 1)
- More articles in Code
<cfif #isdefine(url.action)#><cfelseif #url.action# eq "update">
...do updating stuff here...
</cfif> <cfelseif #url.action# eq "add">
...do adding stuff here...
...here we get the query data to modify...<cfelseif #url.action# eq "modify"> <cfquery name="modify"datasource="#DS#">
select * from table where id = #url.id#
</cfif> Get the data to output...
select * from table </cfquery> Check if we are modifying, if so show the form..
<cfif #url.action# eq "modify"> Show the modify form with the data to change!
<input type="text" name="x"
</cfif> </cfif> Always show the add function so you can always add data...
<input type="text" name="x">etc.... Output the data...(with a link to fill the modify form)
<ahref="list.cfm?action=modify&id=#query.id#">Modify Me!!</a>#query.OtherData# etc...
</cfoutput> </code> If you noticed in the example, This automagically gives you a list of allthe rows in the table, and a link to fill the form to modidy and updatethem. You submit the form and the data is updated, and an accurate listis outputted, ready to be modified and updated again! If you didn't get this on your first look through, just take a piece ofpaper, and walk through it, writing down variable values, and what wouldshow up for the course of action you would take through the application.For example. On a first pass through the page, No matter what, the if"action" has no value, none of the IF statements do anything. All you getis a query, an "add" form, and the master query output. Then pretend youclick a link. Now "action" has a value, so you can go through and see whatwould happen then. In this case it would perform a query to get that rowsdata, then fill a form to modify, then get the master query data thenoutput it again. If you modify the data and submit it, the "action"variable has the update value, so the db is updated with the new info, andthe master query gets that info and outputs it. Now you're ready to goagain! Fun & Easy!!(I hope! =P) Wit this technique you can have a single page control all the actionsassociated with a section of an application, or table, etc.. This can become very helpful when you're dealing with, for example, acontent management application. A single page which can add, delete,modify records from a table will make your life much easier when you orthe user actually start managing the content. And especially if you haveto debug it, its all there for you on one page, ready to go!