|
Creating Database Web Applications with PHP and ASP Authors: Meyer J. Published year: 2005 Pages: 74-77/125 |
The shopping cart application shows the use of many of the database and middleware software features you have learned about in this book. It also gives you a project to build on both to test and enhance your skills and produce an application to suit your requirements.
As you know from doing online shopping, the customer information should have some type of password control. Similarly, the ability to add new products should be restricted to authorized store personnel. You can use your HTML, PHP, and ASP knowledge (see the Exercises and also look ahead to Chapter 16, Scaling Up Your Application) to add features to this project.
Improve the look of the scripts. You might want to consult sources on stylesheets. However, keep in mind that your customers want to see the products and make their orders quickly.
Improve the display by changing either or both of the displaycart function and the money function. You might want to make the money function always return strings of a fixed size .
Change the code to support currencies other than dollars.
Add images to the final Web page presented to the customer: thumbnails of each product ordered.
Add fields to each of the tables and do validation using regular expressions. For example, check for credit card numbers of the format four groups of digits.
Design, program, and debug a set of scripts for the processes of shipping the orders and billing the customers. This would start with reading the orders and ordered items table.
Replace the use of cookies and sessions by the use of another table in the database. You might want to wait until completing the next chapter, which includes use of temporary tables.
Download CD Content
The goal of this chapter is to present in detail scripts for a quiz show application.
Education applications and games are a significant and growing area of the computer and Web industry. The requirements for these applications drive technology in the same way as e-commerce.
The United States state capital application featured in an earlier chapter demonstrated the use of PHP and ASP in quiz-type applications in which information was kept in array variables . The information was in files to be included using the PHP require function and the ASP include instruction. The quiz show application that is the focus of this chapter is based on the use of databases. The databases contain tables holding the questions, data on players and history of player performance. The application makes use of important features in SQL, PHP and MySQL, ASP/JavaScript, and Access.
The critical aspect of the quiz show is the treatment of questions. A table in a database holds a set of questions. Each question has a category, the text of the question, an answer pattern, and a point or cost value. People inputting questions can specify anything as a category. To put it another way, there are no preset categories. Players are presented with a form (see Figure 15.1) in which they enter their names and pick a category. The drop-down list of categories is created dynamically from the distinct categories in the questions table.
Figure 15.1:
Initial screen for quiz show.
The game attempts to find a question to ask the player in the chosen category. The game keeps a history of the questions asked a player, whether the player’s answer was correct or incorrect, and the date. The game has a policy concerning repeating questions. If a person has answered a question correctly, he or she will not be asked it again. If a player has been asked the question on a prior day and answered it incorrectly, the question is put into the pool of acceptable questions. The code selects a question at random from the pool of acceptable questions.
Figure 15.2 shows a screen with a question from the trivia category:
Figure 15.2:
Question from trivia category.
If the player types in a correct answer, the response is as shown in Figure 15.3.
Figure 15.3:
Response to correct answer.
This background is enough to design the database for the application.
The quiz show requires three tables: questions, players, and history. The questions table is also called the question databank . A question record contains more than just the question. One field in a question record is the text of a question; another field is a string that is an answer pattern. Using patterns provides flexibility in evaluating answers. The entity-relationship diagram in Figure 15.4 was previously shown in Chapter 1, “Introduction.”
Figure 15.4:
Entity-relationship diagram for quiz show.
The table that is called the question databank and the players table are not related directly. Instead, each of these tables has a relationship to the history table.
The implementation described in the code in detail supports two main processes: a player plays the game, and someone, called the editor, inserts questions, as shown in Figure 15.5. Two minor processes are showing all the scores and clearing the tables.
Figure 15.5:
Process diagram for quiz show.
As was the case in the previous chapter, the level of detail shown in the process diagram can vary. Here, we have chosen to show the three tables of the database explicitly. The diagram shows all the processes, including the cleartables process used for debugging. The storyboard showing the relationships among the scripts is shown in Figure 15.6 for the PHP implementation.
Figure 15.6:
Storyboard for quiz show.
The storyboard for the ASP implementation would be essentially the same. You can follow the procedure of the shopping cart example and create the tables using Access in stand-alone mode. You can also clear tables using Access. This would allow for more selective editing of the records. Our implementation puts the JavaScript for obtaining a random choice in a distinct file.
The implementation makes use of a temporary table in the askquestion scripts to capture relevant past history on the player. MySQL provides an explicit mechanism for temporary tables. When using Access, the code erases everything in a table called past before populating it with records for the current player.
The askquestion scripts make use of a LEFT JOIN to identify questions that do not have the characteristic of having been asked on the current day or been answered correctly. Review the chapter on SQL if you need more examples of LEFT JOINs.
A question record in the questions table contains the answer as a regular expression pattern. The check is done using the PHP eregi function and the ASP/JavaScript string search method. This is a neat way to make the quiz performance more flexible and less obviously mechanical. However, it does require the person inputting questions to know how to design a regular expression.
|
Creating Database Web Applications with PHP and ASP Authors: Meyer J. Published year: 2005 Pages: 74-77/125 |