Main Page Content
All In One Pages A Technique For Better Code
Need to create a couple functions to, for example, add, delete, modify,records on a table? Ever get confused following the possible chain ofevents through multiple pages when a problem pops up, or during the initalbuild? An easy way to organize it all is to use a common variable which you canpass to a single page, which will determine the action to perform.(I'musing ColdFusion as reference language, this can be done with almost anydevelopment language using the appropriate if statements, etc..) For example. We'll call the page list.cfm. It contains two forms(add &modify), and a section with output from a master query which selectseverything from the db. First you have the check for this master variable, then the query, thenthe forms. This order is used for a specific reason. The query is placedafter the check for the variable to provide that the query returns currentdata. If an add, modify, or delete function existed and was performedafter the query was performed, the output of that query would not includewhatever information was added, what changes were made, or would includedata which was subsequently deleted. That's why the order is important. As you can see below first a check tosee if any actions are to be performed, then the query, and then the formsand the output. Here's the code... <code> Your page simply checks for the variable...
...do updating stuff here...
</cfif> <cfelseif #url.action# eq "add">
...do adding stuff here...
</cfif>
select * from table where id = #url.id#
</cfquery>
</cfif> Get the data to output...
<cfquery name="get"datasource="#DataSource#">
select * from table </cfquery> Check if we are modifying, if so show the form..
<cfif #isdefined(url.action)#>
<cfif #url.action# eq "modify"> Show the modify form with the data to change!
</cfif> </cfif> Always show the add function so you can always add data...
<formaction="list.cfm?action=add">
<input type="text" name="x">etc.... Output the data...(with a link to fill the modify form)
<cfoutput>
<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!
<cfif #isdefine(url.action)#>
<cfelseif #url.action# eq "update">...do updating stuff here...
</cfif> <cfelseif #url.action# eq "add">
...do adding stuff here...
</cfif>
...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#
</cfquery>
</cfif> Get the data to output...
<cfquery name="get"datasource="#DataSource#">
select * from table </cfquery> Check if we are modifying, if so show the form..
<cfif #isdefined(url.action)#>
<cfif #url.action# eq "modify"> Show the modify form with the data to change!
<form action="list.cfm?action=update">
<input type="text" name="x"
</cfif> </cfif> Always show the add function so you can always add data...
<formaction="list.cfm?action=add">
<input type="text" name="x">etc.... Output the data...(with a link to fill the modify form)
<cfoutput>
<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!