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

Report
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
I210 Information Infrastructure I ©Louie Zhu 4 a. Name: compare_integers b. Parameters: int1 and int2 c. Return values: i. -1 if int1 is smaller than int2 ii. 0 if int1 is the same as int2 iii. 1 if int1 is larger than int2 11. Continue the script by creating a PHP variable named random . Set random using an IF … ELSE statement as follows: a. If a post data associated with the hidden form field named random exists, retrieve the data, sanitize it, and then assign it to random . b. If the post data does not exist, assign random a random number between 1 and 20. 12. Check whether a post data associated with a form field named guess exists. Hint: you may use filter_has_var function. This data is the player’s guess. If the data exists, perform followings: a. Retrieve the data, sanitize it, and then store it in a PHP variable named guess . b. Check to see if guess is in the range between 1 and 20. c. If guess is in the range, call the compare_integers function and pass guess and random to the function. Based on the return value, assign a string to a variable named message . i. If the return value is -1, set message to “Your guess ‘ guess’ was too low.” . ii. If the return value is 1, set message to “Your guess ‘ guess’ was too high.” . iii. If the return value is 0, set message to “Congratulations! Y ou guessed the hidden number!!!" d. If guess is not in the range, set message to Invalid guess. Please enter a number between 1 and 20.” 13. Modify the <input> tag for the hidden form field random so its value is the data stored in the PHP variable random . 14. Add PHP code below the form to display the value stored in message in red color. 15. Save the file. Thoroughly test your application. Programming styles Just like any professional document, source code for a computer program needs to be styled by following a set of rules or guidelines. A well-styled program will help programmers to read and understand the code and help avoid introducing errors. Please make sure you leave blank spaces between sections of code and indent your code. To properly indent your code in PhpStorm, choose Sources > Format. In PhpStorm, choose Code > Reformat Code. A well-styled program is also well commented. Add plenty of comments to your code and comment your code as you type it.
I210 Information Infrastructure I ©Louie Zhu 5 Turning in your lab You work will be evaluated on completeness and correction. Thoroughly test your code before you turn it in. It is your responsibility to ensure you turn in the correct files. You will NOT receive any credit if you turn in the wrong files whether you’ve completed the lab 1. Zip the entire Lab07 folder and save it as Lab07.zip . 2. Upload the Lab07.zip file in Canvas before the lab’s deadline. Grading rubric Your TA will assess your lab according to the following grading rubric. You should very closely follow the instructions in this handout when working on the lab. Small deviations may be fine, but you should avoid large deviations. You will not receive credits if your deviation does not satisfy an item of the grading rubric. Whether a deviation is small or large and whether it satisfies the requirement are at your TAs’ discretion. Here is the breakdown of the scoring: Modularizing your code (6 points) Activities Points Create the header.php file. 1 Create the footer.php file. 1 Modify header.php to include a dynamic title (Step 6) and a link to game.php (Step 7). 1 Require header.php and footer.php in index.php , beginningcakephp.php, phpbible.php, books.php, and game.php. 3 Creating the Guessing the Number game (9 points) Activities Points Correctly define the compare_integers function (Step 10). 3 Use IF … ELSE to set a value for random (Step 11). 2 Check if guess exists; if yes, call the compare_integers function and set proper message based on the return value (Step 12). 3 Set the value for the hidden field random (Step 13). 0.5 Echo the value of message (Step 14). 0.5 Programming styles (5 points)
I210 Information Infrastructure I ©Louie Zhu 6 Activities Points Comment your code 3 Use white spaces to separate code sections 1 Indent and line up code 1
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