# PHP & MySQL for Dummies- P8

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
70
lượt xem
8

## PHP & MySQL for Dummies- P8

Mô tả tài liệu

Tham khảo tài liệu 'php & mysql for dummies- p8', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: PHP & MySQL for Dummies- P8

2. 332 Part IV: Applications Figure 11-1: The opening page of the Pet Store Web site. Pet type page The pet type page lists all the types of pets in the catalog. Each pet type is listed with its description. Figure 11-2 shows the pet type page. Radio buttons appear next to each pet type so that customers can select the type of pet that they want to see. Pets page The pets page lists all the pets of the selected type. Each pet is listed with its pet ID, description, picture, and price. The pets page appears in a different format, depending on the information in the catalog database. Figures 11-3, 11-4, and 11-5 show some possible pets pages. Figure 11-3 shows a page listing three different dogs from the catalog. Figure 11-4 shows that more than one pet can have the same pet name. Notice that the house cats have different pet ID numbers. Figure 11-5 shows the output when pets are found in the Color table, indicating that more than one color is available. On all these pages, a line at the top reads Click on any picture to see a larger version. If the customer clicks the picture, a larger version of the picture is displayed. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
3. Chapter 11: Building an Online Catalog 333 Figure 11-2: The pet type page of the Pet Store Web site. Figure 11-3: This pets page shows three differ- ent dogs. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
4. 334 Part IV: Applications Figure 11-4: This pets page shows three cats with the same pet name. Figure 11-5: This pets page shows goldfish that are avail- able in two colors. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
10. 340 Part IV: Applications Listing 11-2 (continued) echo “>$petType”; ➝48 echo “$typeDescription”; ➝49 $counter++; ➝50 } echo “”; echo “ \n”; ➝54 ?> Here is a brief explanation of what the following lines do: ➝12 The include statement brings in a file that contains the infor- mation necessary to connect to the database. I call it misc.inc because that seems more secure than calling it passwords.inc. ➝14 Connects to the MySQL server. ➝18 A query that selects all the information from the PetType table and puts it in alphabetical order based on pet type. ➝20 Executes the query on line 18. ➝33 The opening tag for a form that holds all the pet types. The action target is ShowPets.php, which is the program that shows the pets of the chosen type. ➝35 Creates a counter with a starting value of 1. The counter keeps track of how many pet types are found in the database. ➝36 Starts a while loop that gets the rows containing the pet type and pet description that were selected from the database on lines 19 and 20. The loop executes once for each pet type that was retrieved. ➝38 Separates the row into two variables:$petType and $pet Description. ➝42 Lines 42–43 echo a form field tag for a radio button. The value is the value in$petType. This statement executes once in each loop, creating a radio button for each pet type. This statement echoes only part of the form field tag. ➝44 Starts an if block that executes only in the first loop. It echoes the word “checked=’checked’” as part of the form field. This ensures that one of the radio buttons is selected in the form so that the form can’t be submitted with no button selected, which would result in unsightly error messages or warnings. The counter was set up solely for this purpose. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
12. 342 Part IV: Applications Listing 11-3 (continued) /* Select pets of the given type */ $query = “SELECT * FROM Pet ➝25 WHERE petType=\”{$_POST[‘interest’]}\””; ➝26 $result = mysqli_query($cxn,$query) or die (“Couldn’t execute query.”); /* Display results in a table */ echo “”; echo “ Click on any picture to see a larger version. \n”; while($row = mysqli_fetch_assoc($result)) ➝36 {$f_price = number_format($row[‘price’],2); /* check whether pet comes in colors */$query = “SELECT * FROM Color WHERE petName=’{$row[‘petName’]}’”; ➝42$result2 = mysqli_query($cxn,$query) or die(mysqli_error($cxn));$ncolors = mysqli_num_rows($result2); ➝45 /* display row for each pet */ echo “\n”; echo “ {$row[‘petID’]}\n”; echo “ {$row[‘petName’]}\n”; echo “ {$row[‘petDescription’]}\n”; /* display picture if pet does not come in colors */ if( $ncolors 1 ) ➝65 { while($row2 = mysqli_fetch_assoc($result2)) ➝67 { echo “&nbsp; {$row2[‘petColor’]}
13. Chapter 11: Building an Online Catalog 343 height=’80’ />\n”; } } echo “\n”; } echo “\n”; echo “ See more pets”; ?> Many of the tasks in Listing 11-3 are also in most of the programs in this application, such as connecting to the database, creating forms, and execut- ing queries. Because I document these common tasks for Listing 11-2, I don’t repeat them here. Following is a brief explanation of what some of the other lines do in the program: ➝25 Lines 25–26 select all the pets in the catalog that match the chosen type, which was passed in a form from the previous page. ➝36 Sets up a while loop that runs once for each pet selected. The loop creates a line of information for each pet found. ➝42 Lines 42–45 check whether the pet has any entries in the Color table. Notice that the query results are put in $result2. They couldn’t be put in$result because this variable name is already in use. $ncolors stores the number of rows found in the Color table for the pet. Every pet name is checked for colors when it’s processed in the loop. ➝54 Starts an if block that is executed only if zero or one row for the pet was found in the Color table. The if block displays the pic- ture of the pet. If the program found more than one color for the pet in the Color table, the pet is available in more than one color, and the picture shouldn’t be shown here. Instead, a picture for each color will be shown in later lines. Refer to Figures 11-3 and 11-4 for pet pages that display the pictures and information on a single row, as in this if block. ➝65 Starts an if block that’s executed if more than one pet color was found. The if block echoes a row for each color found in the Color table. ➝67 Sets up a while loop within the if block that runs once for each color found in the Color table. The loop displays a line, including a picture, for each color. Refer to Figure 11-5 for a pet page that displays separate lines with pictures for each color. The page has a link to more pets at the bottom. The link points to the previous program that displays the pet types. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 14. 344 Part IV: Applications Adding pets to the catalog The application that adds a new pet to the catalog should do the following tasks: 1. Create a form that asks for a pet category. The person adding the pet can choose one of the existing pet types or create a new one. To create a new type, the user needs to type a cat- egory name and description. 2. If a new type is created, check that the name and description were typed. 3. Create a form that asks for pet information — name, description, price, picture filename, and color. The person adding the pet can choose one of the existing pet names for the selected category or create a new name. To create a new pet name, the user needs to type a pet name. 4. If new is selected for the pet name, check that the name was typed in. 5. Store the new pet in the PetCatalog database. 6. Send a feedback page that shows what information was just added to the catalog. The tasks are performed in three programs: ✓ ChoosePetCat.php: Creates the pet type form (task 1) ✓ ChoosePetName.php: Checks the pet category data and creates the pet information form (tasks 2 and 3) ✓ AddPet.php: Checks the pet name field, stores the new pet in the cata- log database, and provides feedback (tasks 4, 5, and 6) Writing ChoosePetCat The first program, ChoosePetCat.php, produces a Web page with an HTML form in which the person adding a pet can select a pet type for the pet. ChoosePetCat.php is shown in Listing 11-4. Listing 11-4: Selecting a Pet Type Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 15. Chapter 11: Building an Online Catalog 345 Pet Categories Select a category for the pet you’re adding. If you are adding a pet in a category that is not listed, choose New Category and type the name and description of the category. Press Submit Category when you have finished selecting an existing category or typing a new category. 16. 346 Part IV: Applications Listing 11-4 (continued) ?> ➝59 New Category Category name: Category description: Many of the tasks in Listing 11-4, such as connecting to the database, creating forms, and executing queries, are found in most of the programs in this appli- cation; refer to Listing 11-2 for an explanation. The following list provides a brief explanation of what the following lines do: ➝37 A query (lines 37 and 38) that selects all the pet types from the PetType table and sorts them in alphabetical order. ➝45 Creates a counter with a starting value of 0. The counter keeps track of how many pet types are found in the database. ➝46 Starts a while loop that executes once for each pet type. The loop creates a list of radio buttons for the pet types, with one button selected. Here are the details of the while loop: ➝49 Echoes a form field tag (lines 49 and 50), including a label tag, for a radio button with the value equal to$petType. This statement executes once in each loop, creating a radio button for each pet type. This statement echoes only the first part of the form field tag. ➝51 An if block that executes only in the first loop. It echoes the attribute “checked=’checked’” as part of the form field. This attribute ensures that one of the radio buttons is selected when displayed so that the form can’t be submitted with no button selected, which would result in unsightly error messages. The counter was set up solely for this purpose. Although adding “checked=’checked’” to every radio button works in some browsers, it causes problems in other browsers. The extra programming required to add “checked= ’checked’” to only one radio button can prevent problems. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
18. 348 Part IV: Applications Listing 11-5 (continued) addNewType($_POST[‘newCat’],$_POST[‘newDesc’],\$cxn); } } ➝31 include(“NewName_form.inc”); ➝32 ?> Only some of the lines are documented in the following list because many of the tasks in the listing are found in most of the programs in this application. The common tasks are documented for Listing 11-2 and explained in other parts of the book, so I don’t repeat them here. Here’s a brief explanation of what the following lines do in the program: ➝7 Starts an if statement that checks whether the user clicked the submit button labeled Cancel or Return to category page. If so, it returns to the first page. ➝13 Includes the file that defines the function AddNewType(), which is used later in this program. The function is shown in Listing 11-8. ➝14 Creates a connection to the database. ➝18 Starts an if block that executes only if the user selected the radio button for New Category in the form from the previous program. This block checks whether the new category name and descrip- tion are filled in. If the user forgot to type them, he or she is asked for the pet type name and description again. After the name and description are filled, the program calls a function that adds the new category to the PetType table. The following lines describe this if block in more detail: ➝20 Sets the category name, which currently equals “new”, to the new category name. ➝21 Starts an if block that executes only if the category name or the category description is blank. Because this if block is inside the if block for a new category, this block exe- cutes only if the user selected New Category for pet type but didn’t fill in the new category name and description. ➝24 Creates a form that asks for the category name and description. The HTML for the form is included from a file — NewCat_form.inc, which is shown in Listing 11-6. This executes only when the if statement on line 21 is true — that is, if the category is new and the category name and/or description is blank. ➝25 Stops the program after displaying the form on line 24. The program can’t proceed until the category name and description are typed. This block repeats until a category name and description are filled. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.