Skip to page content or Skip to Accesskey List.

Work

Main Page Content

All In One Pages A Technique For Better Code

Rated 3.74 (Ratings: 1)

Want more?

  • More articles in Code
 

Mike Roberto

Member info

User since: 07 Sep 1999

Articles written: 1

Need to create a couple functions to, for example, add, delete, modify,

records on a table? Ever get confused following the possible chain of

events through multiple pages when a problem pops up, or during the inital

build?

An easy way to organize it all is to use a common variable which you can

pass to a single page, which will determine the action to perform.(I'm

using ColdFusion as reference language, this can be done with almost any

development 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 selects

everything from the db.

First you have the check for this master variable, then the query, then

the forms. This order is used for a specific reason. The query is placed

after the check for the variable to provide that the query returns current

data. If an add, modify, or delete function existed and was performed

after the query was performed, the output of that query would not include

whatever information was added, what changes were made, or would include

data which was subsequently deleted.

That's why the order is important. As you can see below first a check to

see if any actions are to be performed, then the query, and then the forms

and the output. Here's the code...

<code> Your page simply checks for the variable...

<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"

value="#modify.x#"> etc...
</cfif> </cfif>

Always show the add function so you can always add data...
<form

action="list.cfm?action=add">
<input type="text" name="x">

etc....

Output the data...(with a link to fill the modify form)
<cfoutput>

<a

href="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 all

the rows in the table, and a link to fill the form to modidy and update

them. You submit the form and the data is updated, and an accurate list

is outputted, ready to be modified and updated again!

If you didn't get this on your first look through, just take a piece of

paper, and walk through it, writing down variable values, and what would

show 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 get

is a query, an "add" form, and the master query output. Then pretend you

click a link. Now "action" has a value, so you can go through and see what

would happen then. In this case it would perform a query to get that rows

data, then fill a form to modify, then get the master query data then

output 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, and

the master query gets that info and outputs it. Now you're ready to go

again! Fun & Easy!!(I hope! =P)

Wit this technique you can have a single page control all the actions

associated with a section of an application, or table, etc..

This can become very helpful when you're dealing with, for example, a

content management application. A single page which can add, delete,

modify records from a table will make your life much easier when you or

the user actually start managing the content. And especially if you have

to debug it, its all there for you on one page, ready to go!

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.