Lab 7

.pdf

School

Indiana University, Purdue University, Indianapolis *

*We aren’t endorsed by this school

Course

210

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

6

Uploaded by HighnessYakMaster1036

I210 Information Infrastructure I ©Louie Zhu 1 Lab 7: Use functions and file inclusion in the PHP Online Bookstore In this lab, you will modify the PHP Online Bookstore application you created in a previous lab. There are two major tasks: 1), create library files for header and footer to modularize the Web site; 2), create the Guessing the number game using functions and conditional statements. Preview the complete application at https://i210.sitehost.iu.edu/ . The following is a screen shot of the site’s homepage. The page consists of three sections: the header, the main body, and the footer. To make the site look consist, the header and footer appear on every page on the site. Only the main body contains page-specific content. Getting ready 1. Download the Lab07.zip file from Canvas and extract it into the htdocs/I210 folder on your computer. The extraction should create a folder named Lab07 with six PHP files and two subfolders in it. 2. Start PhpStorm and run the Lab07/index.php file in the I210 project. Click the List Books link from the navigation bar and then the Beginning Cake PHP and PHP Bible links to display the details pages. Part 1: Modularizing your code To better reuse and manage code, code for the navigation bar and banner needs to be stored in a library file named header.php ; code that creates the horizontal line and copyright information at the bottom of the pages needs to be stored in another library file named footer . php . Store the two files in the includes folder. Main body: content is page specific content footer header
I210 Information Infrastructure I ©Louie Zhu 2 Create the header.php and footer.php files 1. Create two PHP files (not PHP Web pages) inside the existing includes folder and name them header . php and footer . php . Delete all code in the two files. Modify the index.php file 2. Open index.php file in PhpStorm. Add your name and today’s date in the head section. Move all lines of code above the HTML comments that read “page specific code starts” into the header . php file. Move all lines of code below the HTML comments that read “page specific code ends” to the footer . php file. 3. At the top of the index.php page, add a new PHP code block. Inside the code block, two things need to be done: a. Create a PHP variable named title and initialize the variable with the page title “PHP Online Bookstore Home”. b. Use the PHP command require to include code from the header . php file. Note that the header . php is stored in the includes folder. 4. At the bottom of the index.php page, add a new PHP code block. Inside the code block, use the PHP command require to include code from the footer . php file. Note that the footer . php file is stored in the includes folder. Modify the header.php file 5. Switch to header . php in PhpStorm. 6. Modify the HTML title tag so the page’s title is the output of echoing the PHP variable named title . 7. In the navigation bar, add a hyperlink that links to the game.php file. Modify other PHP Web pages 8. For all other PHP Web pages, you need to delete the code that is already in header and footer, and then use the instructions in Steps 3 & 4 to require the header.php and footer.php files. Please use following titles for those Web pages: a. beginningcakephp.php Beginning Cake PHP b. phpbible.php PHP Bible, 2 nd Edition c. books.php Books in Our Store d. game.php Guessing the Number Game
I210 Information Infrastructure I ©Louie Zhu 3 Part 2: Creating the Guessing the Number game The game starts with generating a random number between 1 and 20 (Note: to make the game more challenging, you can use a wider range). The random number only needs to be generated once, when the game just starts, and is available throughout the whole game session by posting it back to the page using a hidden form filed. The player plays the game by typing a number into the textbox and then pressing the Guess button. The application then compares the player’s guessed number with the random. The message “Y our guess guess’ was too high.” or “Your guess guess’ was too low.” displays if the guessed number is larger than or smaller than the random. If the player’s guess is correct, the messa ge displays Congratulations! You guessed the hidden number!!! ”. 9. Open game.php file in PhpStorm. Review the current code. You will notice a form containing a text box, a button, and a hidden field. Please note these important things about the form and form elements: a. The form uses the “post” method to post data. b. The form posts data back to the page that contains the form (the page itself). c. The text box named guess is a number field and accepts player ’s guess. It is a required filed. d. The hidden field random stores the system-generated random number. Defining the function that compares two integers 10. Inside the PHP code block at the top of the script file, define a function. The function should compare the two parameters and return a number (-1, 0, or 1) that indicates whether the first parameter is smaller than, larger than, or the same as the second parameter. Define the function using following settings:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help