A master-detail page pairing is necessary for the update and deletion functionality, because you have to allow the users to select which country they want to update or delete. In this task, you'll create a simple master-detail page consisting of a table with each country's name, a link to modify the country, and a link to delete the country in a row. As before, each of these links will pass a URL parameter identifying the country that the user has selected.
Open admin_cp_master.asp, and switch to code view.
As before, you'll create the recordset before laying out the page, and since you'll handwrite the code necessary to create the recordset, you need to be in code view.
Between the Restrict Access server behavior code and the beginning of the document itself (just before the <?xml> tag), enter the code necessary to create a recordset ("rs_countries") that retrieves the country name and ID of every record in tbl_country.
[View full width]<% Dim dbConn Dim rs_countries set dbConn = Server.CreateObject("adodb.connection") dbConn.open("newland") Set rs_countries = dbConn.Execute("SELECT countryID, countryName FROM tbl_country ORDER BY countryName ASC") %>
<cfquery name="rs_countries" datasource="newland"> SELECT countryID, countryName FROM tbl_country ORDER BY countryName ASC </cfquery>
Aside from the details of the SQL statement, this code is identical to that used earlier in this lesson, so you should understand it. The query itself is also easy to read: It retrieves the country name and unique ID, ordered by country name.
ASP users only: Add the code to close and destroy the recordset at the end of the document.
<% rs_countries.Close() Set rs_countries = Nothing %>
Again, this code prevents the recordset from wasting memory on the server.
Switch to design view, and type the following two lines of text:
Select a country to modify or delete.
Caution: Deleting is instant and permanent.
Again, providing directions for the user is critical to the success of your applications.
Return to code view, and below the paragraph you just created, add the code to create a new table with one row and three columns.
<table width="98%" border="1" cellspacing="0" cellpadding="3"> <tr> <td>XX</td> <td>XX</td> <td>XX</td> </tr> </table>
This creates the basic framework for the table. When you are done with it, its contents will be dynamically generated, with each record being mapped to a single row. Since you can't know the number of rows in advance, you'll have to loop through the recordset, creating a new row for each record.
Enter the appropriate code needed to create a looping structure that encloses the <tr> tag. The code should wrap outside the opening and closing <tr> tags.
<% Do Until rs_countries.EOF %> <tr> <td>XX</td> <td>XX</td> <td>XX</td> </tr> <% rs_countries.MoveNext() Loop %>
<cfoutput query="rs_countries"> <tr> <td>XX</td> <td>XX</td> <td>XX</td> </tr> </cfoutput>
In this step, you are in effect creating a repeat regionyou just aren't using the Dreamweaver behavior. And in spite of their cosmetic differences, both ASP and ColdFusion use remarkably similar logic. Both have a block of code before and after the section to be looped over (the <tr> section). Both specify a query and create a loop that breaks only when the recordset runs out of records: ASP's EOF accomplishes this explicitly, while the recordset loop is implied in ColdFusion, when you specify the query attribute in a <cfoutput> tag.
Next, you need to build the contents of the table cells, which will contain a mix of HTML and ASP or ColdFusion code.
Create the static HTML structure using placeholders for the content inside the <td> tags.
[View full width]<td>Country Name</td> <td><a href="admin_cp_update.asp?countryID=CountryID">Modify this country's ®profile</a></ td> <td><a href="admin_cp_delete_processor.asp?countryID=CountryID">Delete </a></td>
This code is the same for both ASP and ColdFusion (except that the file extensions in the href attribute for ColdFusion should be .cfm rather than .asp), because it is merely static HTML. We're using placeholders here to ensure that we get the HTML syntax right, before adding the dynamic code.
Replace the three placeholders with dynamic output, as follows.
[View full width]<td><%=rs_countries("countryName")%></td> <td><a href="admin_cp_update.asp?countryID=<%=rs_countries("countryID")%>"> Modify this country's profile</a></td> <td><a href="admin_cp_delete_processor.asp?countryID=<%=rs_countries ("countryID")% >">Delete</a></td>
[View full width]<td>#countryName#</td> <td><a href="admin_cp_update.cfm?countryID=#countryID#">Modify this country's profile</ a></td> <td><a href="admin_cp_delete_processor.cfm?countryID=#countryID#">Delete </a></td>
You've seen plenty of ASP and ColdFusion output code in the course of this book, so the code should be easy to read, especially if you remember that <%= in ASP is equivalent to <% Response.Write().
Save, upload, and test the file in a browser.
The table has as many rows as tbl_country has records. If you hover your mouse over one of the links, you'll see not only the URL, but also the countryID parameter attached with the correct country ID.