If you read the title of this section, chances are you're wondering why we're covering ASP, ColdFusion, and PHP in a single section. The answer to this question is simple; Dreamweaver's core functionality is so similar across the three server models that individual sections for each server-side technology aren't warranted. Instead, we'll build our dynamic web pages using the ASP server model, pointing out the small differences/additions for the other two server models along the way. In the coming sections, you'll learn to build the dynamic employee store web page using ASP, ColdFusion, or PHP. As the chapter unfolds you'll learn about the various Dreamweaver features that make working with database data a snap. Specifically, you'll learn about the following concepts:
By the end of this chapter, you'll have a fully functional employee store web page that pulls product information directly from a database and displays it for users to purchase. Let's get started. Creating the EmployeeStore PageThe EmployeeStore page of the Dorknozzle intranet site is the heart of the Dorknozzle web application. After all, this is where Dorknozzle employees come to buy flare and chochkes (marketing lingo for lame and unusable stuff) to show off their Dorknozzle pride to co-workers, friends, and family. The idea is that employees will visit the page, the page will pull information from the EmployeeStore table contained in the Dorknozzle database, and present items to the user for purchase. The user can then click an item, add it to their cart, and complete their purchase using a convenient checkout button. All of this functionality will be exposed in time. For now, let's review the basic structure of our Dorknozzle database, specifically, the EmployeeStore table. As Figure 25.18 indicates, we can expand the EmployeeStore table directly from the Databases panel (by clicking the small (+) icon) to reveal the field names and the data types associated with each field. Figure 25.18. The EmployeeStore table is the central source of data for the EmployeeStore web page.Table 25.1 outlines the fields in the database table, including the data types used by Dreamweaver.
Now that you've seen the database structure, you are ready to begin extracting data from it. The next few sections cover the following concepts as they relate to ASP, ColdFusion, and PHP:
Creating a RecordsetBy now you may be curious about how data in your database can be extracted into your application. Sure, you've learned a lot about SQL and are familiar with the commands to retrieve the information, but now what? SQL alone does not provide enough flexibility to read from the database and write that data to the application; there's still a piece of the puzzle missing. That piece is the recordset. Recordsets act as an intermediary virtual table between the database and the application. You can write SQL commands to ask questions of the database (also known as a query), but the information retrieved is stored in a recordset. The programming logic iterates through the recordset and ultimately presents the data to the browser in a structured way. Figure 25.19 illustrates this point. Figure 25.19. The application makes a call to the database, which then returns data in the form of a recordset.Remember that the questions asked of the database are made in the form of queries, and queries are a process that usually involve SQL to structure how the question will be asked. After the question (query) has been asked, the data is returned into a virtual table or recordset. The recordset is then browsed through by the application logic and presented to the user in a structured HTML document. To demonstrate this point, you can use Dreamweaver to create a simple recordset that will query the EmployeeStore table. To create a recordset, follow these steps:
Note that by selecting the Bindings tab and then expanding the recordset, you can view the field names contained within the recordset (more on this later). Creating an Advanced RecordsetSimple recordsets can serve your needs if you are performing simple queries of all or certain fields in a single database table. But what if you wanted to perform joins and merge two tables into one recordset? Unfortunately, the simple method wouldn't do. Although creating advanced recordsets can become very complex, the trade-off is flexibility, scalability, and power. Rather than creating multiple recordsets in which you store each and every table, you can join two or more tables into one recordset based on a common value. Take the EmployeeStore and Orders tables as examples. Suppose that you were working in the Shipping and Receiving department and your job was to print out a list of all orders for a specific day. If that were the case, you would need to create a recordset that merged the EmployeeStore and Orders tables together into one virtual table. Furthermore, you'd also need to include the Employees table because all orders are related not only by products in the EmployeeStore table, but also by the employee from the Employees table. This complex recordset can't be constructed using the Simple recordset view (the basic Recordset dialog). Instead, you'd have to use the Advanced view. To create a recordset using the Advanced view, follow these steps:
After you close the Recordset dialog, notice that the new recordset is listed in the Bindings panel. Creating a Recordset from a ViewIn the preceding sections, you saw how to create both simple and advanced recordsets. The Simple Recordset dialog allowed you to select a single table and work with the fields in that table in your recordset. The Advanced Recordset dialog allowed you to paste in SQL statements created in Access's Query Designer to join two or more tables and work with the data from multiple tables in your recordset. Recall that you saved the query you created in Access as EmployeesOrders. Look at the saved query in Access; it might resemble Figure 25.28. Figure 25.28. The query is saved as EmployeesOrders in Access.Rather than opening Access every time we need to use that query and then copying the SQL statement and pasting it in the Advanced Recordset dialog, we can use that query in Dreamweaver as-is. To use the saved query in Dreamweaver, follow these steps:
NOTE We explored the process of creating saved queries within Access, but you should know that SQL Server/MSDE and the newest build of MySQL 5 also support views. Nothing should stop you from using Web Data Administrator to create views in MSDE. Unfortunately, however, as of the time of this writing, there is no visual tool that supports building views for MySQL 5. Whether you are creating simple or advanced recordsets in Dreamweaver or using a saved query to create your recordsets, be assured that the process remains relatively simple. As you have seen, even complex joins can be achieved with ease. The next sections introduce you to methods of extracting the data from the recordset into your application. Working with Dynamic ElementsNow that you've been able to extract data from your data source, your next step is to structure it within your application somehow. Dreamweaver's Server Behaviors and Bindings panels provide the capabilities you need to get started producing dynamic elements centralized within the database but exposed by the application. Dynamic TextThe first step to creating dynamic applications is to try and make all your text as dynamic as possible. That is, allow all your company's valuable information to reside in the database and then pull it out, displaying it in dynamic pages as needed. To start creating dynamic text, begin by creating a table that will serve as the means of organizing the data output. You can accomplish this by following these steps:
Congratulations! You've just taken your first step to working with dynamic web pages! As the chapter unfolds, you'll learn to add dynamic images, repeating regions, pagination functionality, and more. This is just the tip of the iceberg. Dynamic ImagesNow that you've created dynamic text within your application, you're ready to begin adding dynamic images. The images we'll add here are not the typical static images you have used throughout the book; instead, they will be dynamic. Remember, we added the path to the image of each item in the EmployeeStore table. What we want to now do is dynamically show the image, based on the path provided within the ImageURL field, on our dynamic EmployeeStore web page. To do this, follow these steps:
Save your work and test the results in the browser by pressing the F12 key. The image for the Dorknozzle T-Shirt should now appear as shown in Figure 25.33. Figure 25.33. The image is dynamically placed based on the path within the database.Recordset PagingNow that you've seen how easy it is to place dynamic content on a page, you'll probably want to begin adding features that allow your users to interact with the content on your pages. Paging, for example, enables your users to page through items in the recordset until they find the item they are looking for. For every press of a button, your users can advance to the next record or, conversely, return to a previous record. Developers gain certain benefits from pagination, including:
Dreamweaver's pagination behaviors are located in the Add (+) menu in the Server Behaviors panel and include the following behaviors:
NOTE In ColdFusion, the pagination behaviors are named a bit differently. For example, ColdFusion uses Move to First Page, Move to Previous Page, Move to Next Page, and Move to Last Page. To begin adding pagination features to your site, follow these steps:
Save your work and test the result in the browser by pressing the F12 key. You should be able to navigate from the first record in the EmployeeStore database to the next record and back. Figure 25.36 shows the record Dorknozzle Hooded Sweat Shirt that appears after Dorknozzle Shirt as a result of clicking Next. Figure 25.36. Clicking the Next hyperlink enables you to cycle through records.Of course, you should feel free to experiment with the other paging server behaviors. Dreamweaver provides behaviors for moving to the last record and to the first record. If you're using the ASP model, you can also include a text box that allows a user to enter the specific record to jump to. Showing Specific RegionsNow that you have added the capability to cycle through the recordset, consider the following problem: Users click Next until they get to the last record and then they are abruptly stopped. They keep clicking Next but nothing happens. The problem is that the users have reached the end of the recordset and they cannot go any further. Of course, the user has no way of knowing that. To fix this, Dreamweaver provides functionality in the form of a group of Show Region behaviors. For example, we can create a region that contains text alerting the users that they have reached the end of the recordset. When the user reaches the recordset's end, the region is displayed. The complete list of Show Region behaviors is given here: NOTE Although the functionality is the same across server models, the names of these server behaviors vary slightly under the ColdFusion and PHP server models.
NOTE In ColdFusion, the conditional behaviors are named a bit differently. For example, ColdFusion uses Show Region if First Page, Show Region if Not First Page, Show Region if Last Page, and Show Region if Not Last Page. To add a Show Region behavior to your page, follow these steps:
Notice that a gray visual aid surrounds the text. This guarantees that the text contained within this region appears only when the user reaches the last record in the recordset. Save your work and run it in the browser by pressing the F12 key. Click the Next hyperlink until you reach the last record. A message will appear when you reach the end of the recordset. Again, this is only one example of the server behaviors exposed in the Show Region set of server behaviors. Now that you have a general idea about what the Show Region server behaviors do, feel free to experiment with the others. Using Repeat RegionAlthough recordset paging is the ideal model to strive for, at times you might want to display all the records (or a certain number of records) in the database at once. The Repeat Region behavior enables you to create a pattern that repeats within the web page. For instance, in the EmployeeStore page, a table was created to display the content for the image, name, description, and cost. Using the Repeat Region server behavior, you can maintain that structure and repeat the contents for every record (or a certain number of records) in the database. To create a repeatable region on our page, follow these steps:
Save your work and test the results in the browser by pressing the F12 key. Figure 25.39 shows records grouped in sets of 5. Figure 25.39. All the records are shown when you use the Repeat Region server behavior.As you can see, you can scroll down the page to view five Dorknozzle items at a time. When you want to view the next five items in the recordset, click the Next hyperlink that we added in the previous section. NOTE If you're working with the ColdFusion model, you probably noticed that the paging server behaviors didn't work. Try them now. You'll notice that by adding the Repeat Region server behavior, the paging server behaviors now work. Again, ColdFusion relies on the Repeat Region server behavior for the paging server behaviors to work properly. |