We have reached the final installment of this particular series. In this installment, we will explore how you can write a function to return and print the results of a form (actually, it doesn’t have to be a form, this function will simply pull information out of a database and print it on the screen for you).
As in the previous installment, I will be using a few outside functions to format specific information nicely, etc., and we will be using a database. I find that pulling the information out of the database is the best way to show the results of a form, because it’s going to show you exactly what was stored.
In the past, I used to just show the results by using the $_POST or Request.Form stacks (PHP and VBScript respectively). However, I started to realize that those types of results were inaccurate, as they only showed what the user inputted, rather than showing the information that was being stored. Generally that’s fine, but some times that can cause serious problems. For instance, if you set a character limit on an input, and you impose that limit when processing the form, that wouldn’t be reflected by simply printing the form information.
Let’s start with the code:
<%
function showResults(idnum)
Set rs = Server.CreateObject("ADODB.RecordSet")
openCon
rs.Source = "SELECT * FROM `" & maintable & "` WHERE `" & idname & "` = " lastid
rs.Open
For Each myKey in rs.Fields
If not(myKey.Name = idname) Then
%>
<div class="label"><%=formLabels(myKey.Name)%></div>
<%
If(myRegExTest(myKey.Name,"phone") OR myRegExTest(myKey.Name,"fax")) Then
%>
<div class="result"><%=txtPhoneReturn(rs.Fields(myKey.Name))%></div>
<%
else
%>
<div class="result"><%=rs.Fields(myKey.Name)%></div>
<%
End If
End If
Next
end function
%>
That’s all there is to it.
Now, let’s pick it apart a little bit to show you what’s going on.
<%
function showResults(idnum)
Set rs = Server.CreateObject("ADODB.RecordSet")
openCon
rs.Source = "SELECT * FROM `" & maintable & "` WHERE `" & idname & "` = " idnum
rs.ActiveConnection = connname
rs.Open
I open the function, then set my recordset object. I call the openCon function, which, as I explained in my previous post, simply sets a connection called connname and opens it, and then sets the cursortype and locktype of my recordset. Finally, I set connname as the connection object that’s associated with my recordset and I set my SQL statement. The variable “maintable” is the name of the table that I’m opening. The variable “idname” is the name of my primary key in that table. Finally, the variable “idnum” is the value of the primary key that’s associated with the row I’m pulling from the database.
In order to get that ID from a new set of information, we simply grab the “MAX(idname)” from the database after we store it in the DB. That was the last portion of the code in my last post.
For Each myKey in rs.Fields
If not(myKey.Name = idname) Then
In the code above, I’m starting a loop that’s going to walk through all of the columns in my DB row. After that, I’m simply checking to make sure I don’t print my primary key. You certainly can if you feel the need or want to do so, but in most of my cases, that is an arbitrary number that has no impact on my users. Therefore, there’s no reason to print it out.
%>
<div class="label"><%=formLabels(myKey.Name)%></div>
<%
Now, I’m printing the label associated with that column from the DB. I have stored all of my labels in a scripting dictionary (or, in PHP, they’d be in an associative array).
If(myRegExTest(myKey.Name,"phone") OR myRegExTest(myKey.Name,"fax")) Then
%>
<div class="result"><%=txtPhoneReturn(rs.Fields(myKey.Name))%></div>
<%
Here, I’m using a custom function called “myRegExTest” that simply checks to see if the pattern (which is the second parameter passed to the function) exists inside of the subject (the first parameter). In this particular case, I’m checking to see if the name of my columns contain “phone” or “fax”. I’m obviously assuming that any columns with those strings in their name would contain a phone or fax number. I’m then calling a custom function to format that number correctly while I print it.
else
%>
<div class="result"><%=rs.Fields(myKey.Name)%></div>
<%
End If
End If
Next
end function
%>
Finally, if the column title is not a phone or fax number, and it’s not my primary key, then I print it out just the way it is. I then end my if statements, my loop and my function.
The items that I print out are printed inside of classed divs. The labels are printed inside of a div with the class of “label”, and all of the results are printed inside of a div with the class of “result”. You can then make them look however you want.
Post Your Comment