If you have used Dreamweaver to create dynamic pages, you are probably familiar with the Repeat Region server behavior and the concept of Next/Previous records. You are probably familiar with the Master/Detail pageset concept: the Master page contains a table listing your records with links that allow you to drill down into a Details page. The Details page gives you the details about a particular record. Unfortunately, the Details page does not allow you to create the Previous/Next links easily. This is often desirable in a site, such as in a photo album, real estate listing, or other type of list where the user might want to remain on a Detail page and simply cycle through the records rather than go back to the Master page again. This tutorial will show how to create a SQL stored procedure that will supply Previous/Next links to any ColdFusion or classic ASP page.
First, you'll need a sample database table to use. I'll use a simple blog table named blog_items that was also used in the series on creating a simple blog. To create the table, run the following script in SQL Server Query Analyzer. The table can be placed in any database, or a blank database. (A full script for the table and stored procedure is available in the download at the end of the tutorial.)
CREATE TABLE blog_items (
blog_item_id int IDENTITY (1, 1) NOT NULL ,
blog_item varchar(1000) NULL ,
blog_item_datetime datetime NULL ,
blog_item_title varchar (255) NULL
)
And you'll need some sample data:
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('There is a storm coming!','1/2/2002 3:45:00 PM','Bad weather today')
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('Help! Snow is on the way!','1/3/2002 3:45:00 PM','Storm on the horizon')
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('Thankfully the storm has passed us by!','1/4/2002 3:45:00 PM','The storm has passed')
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('There is a another storm coming!','1/2/2002 3:45:00 PM','Another storm coming!')
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('The storm is here! Snow is falling and we are cold.','1/3/2002 3:45:00 PM','It is here!')
INSERT blog_items (blog_item, blog_item_datetime, blog_item_title)
VALUES ('The storm is over. Now we have to dig out of the snow.','1/4/2002 3:45:00 PM','Buried in the snow')
The tutorial assumes you have used Master Detail Page Sets before, but I'll run through the steps to create the basic page so that you can follow the rest of the tutorial. Sample master/detail pages are included in this article's support files.
SELECT * FROM dbo.blog_items ORDER BY blog_item_datetime DESC
Figure 1 The Master Detail Page Set dialog box
SELECT blog_item_id, blog_item_datetime, blog_item_title
FROM dbo.blog_items
ORDER BY blog_item_datetime DESC
If you browse the Master page now, you should be able to drill down to the Detail page.
When you have a repeat region, you can cycle through records easily using the Recordset Navigation Bar. When you have a Details page, it is not so easy because your recordset only contains one record: the SQL statement uses a WHERE clause to pull only one record. You cannot get the previous or next record easily, because the primary key may or may not be an integer, and it may or may not be sequential. For example, if a record is deleted, even if the primary key is a sequential integer there will be a hole in the list of records. The only way to accurately retrieve the previous and next link is to run two separate queries: one to retrieve the primary key closest to the current primary key but smaller, and one to retrieve the primary key closest to the current primary key but larger. The following stored procedure will do that, and return the two results as parameters.
The stored procedure we'll use, spGetNextAndPreviousItems, has one input parameter (the current primary key) and two output parameters (the previous primary key and the next primary key). The stored procedure can be created in Query Analyzer or SQL Server Enterprise Manager:
CREATE PROCEDURE spGetNextAndPreviousItems
@id int,
@prev int output,
@next int output
AS
SELECT top 1 @prev = blog_item_id
FROM blog_items
WHERE blog_item_id< @id
ORDER BY blog_item_id DESC
SELECT top 1 @next = blog_item_id
FROM blog_items
WHERE blog_item_id> @id
ORDER BY blog_item_id
One point worth mentioning is that if the current record (the @id parameter) is the first record in the table, the resulting output parameter (@prev) will be null. Similarly, if the current record is the last record in the table, the resulting output parameter (@next) will be null. This allows you to put conditional regions around your previous and next links on the page.
Note: The stored procedure uses output parameters for convenience, but could have also been written to use a SELECT statement which would have returned a recordset to your calling page.
You can test the stored procedure in the Query Analyzer as well, by using some code like the following:
DECLARE @MyOutputPrev int
DECLARE @MyOutputNext int
EXEC spGetNextAndPreviousItems
@id = 3,
@prev = @MyOutputPrev OUTPUT,
@next = @MyOutputNext OUTPUT
SELECT @MyOutputPrev AS Prev_Item, @myOutputNext AS Next_Item
Change the value of the @id variable to see the different previous and next items in the table.
To call the stored procedure, simply pass the current record id to the procedure. Dreamweaver has a built-in server behavior in the Bindings panel that allows you to automate the process. The parameters are a little different for each server model, so I'll do ColdFusion first:
Figure 2 The ColdFusion version of the Stored Procedure dialog box
For ASP, follow these steps:
Figure 3 The ASP version of the Stored Procedure
To create the links, complete the following steps for the Details page for both ASP and ColdFusion:
Figure 4 Adding a parameter to the link
Figure 5 Adding the dynamic variable for the link
The final step is to hide the links if you are on the first or last record. The code is different for ASP and ColdFusion, as shown below:
Around the Previous link:
<%If Command1.Parameters.Item("@prev").Value <> "" Then %>link<%End If%>
Around the Next link:
<%If Command1.Parameters.Item("@next").Value <> "" Then %>link<%End If%>
Around the Previous link:
<cfif out_prev NEQ "">link</cfif>
Around the Next link:
<cfif out_next NEQ "">link</cfif>
Now, if you browse the master page, you can drill down to the detail page and be able to navigate directly from the detail page using the Previous and Next links. This stored procedure can be easily modified to work with any database table.
Dreamweaver has some navigation code built into it for repeat regions, but nothing for navigating a details page. This tutorial has shown one way to do it with a stored procedure that is easy to implement and versatile enough to use with any database table.