Syllabus Schedule Project Labs Canvas Server Access phpMyAdmin Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9

Assignment 3 -- Control structures, Functions

The information needed to complete this assignment is covered in Nixon chapters 4-5. I suggest that you have your copy of Nixon available as a reference while you work on your labs.

You may use the HTML from the samples. Name your scripts with the file names used below.

  1. ForLoop.php -- Use a for loop (w3Schools example) that iterates the number of times specified by the user.

  2. ForLoopTable.php --Row data is usually displayed in a table. Modify the previous exercise to write your script output to a table.

  3. ForLoopNested.php -- Add some columns to your table. Copy the previous exercise and modify it to write a table with multiple rows and columns. Write the row and column number in each cell. 
    • Tip: Modify the previous exercise by nesting a for{} loop inside the existing for{} loop . The inner loop will produce the columns.

  4. ValidInput.php -- It is important to validate all user entries so that invalid data does not cause our script to crash or misbehave. Modify this script to alert the user if they input a non-numeric input or a value greater than 10. Use the is_numeric() function to check for numeric data. Us an if statement to check for numeric and to check that the values are within the range 1 to 10 (inclusive). It will look something like this:
                $rows = $_GET['rows'];
                if (!empty($rows)) {
                     //validate with is_numeric and < > tests
                     if(test1 && test2 && test3){
                        //valid entry
                        //use the value
                     else {
                        //invalid entry
                        echo "Please enter a number 1-10";

  5. SalesCalc.php -- Write a script that computes the sales information shown in the example. Validate the form input data to make sure it is numeric. Do not show a warning when the page initially loads. Tips:
    • Use two nested if statements to validate the data as in the previous exercise. The first checks to see if anything has been entered and does not produce a warning if empty (for when the page initially loads). The second checks the datatype and produces a warning if not numeric. The HTML table that displays the calculations is inside the code block for the if statements.
    • Use the number_format() function to format the numeric output in a currency format. Google "php number_format" for documentation.

  6. FontPicker.php -- Modify the class example FunkyFont.php (source) to use five additional fonts. Steps:
    • All the images are stored on Yorktown and may be referenced from their current location (no need to copy the images). The common portion of each path is:

      "<img src='/sandvig/images/alphabet/"

    • The images for each font is stored in a separate folder. Each font uses a slightly different naming convention. The naming convention for each font is (where x is the character desired) is shown in the table below. Look at the image tags in the sample to see a complete image path.
Font Directory & File
Chunk Red chunk/red/x9.jpg
Deco Blue deco/blue/x1.gif
Animals animals/x4.gif
Elegant Red elegant/red/4x.gif
Funky funky/x3.jpg
Tape Punch punch/black/x7.gif
    • You may copy the HTML for the radio buttons from the sample.
    • Use a series of "if" "elseif" statements or a switch statement to display the appropriate font.
    • The readability of your code can be improved significantly by using a function to break out the code that selects & writes the image tags. For instance, the sample script uses a function fWriteChar($char, $font). It accepts a single character and the name of the font and writes the appropriate image tag.
    • Replace spaces in the message with <br /> tags.
    • Optional: For code beautification include line breaks (\n) in your echo statements.
    • Optional: The sample includes a snippet of JavaScript that resets the radio buttons to the selected item after each page postback. Copy this JavaScript or write your own and add it to the page. The sample JavaScript requires that the radio buttons have the name "font." Execution of the JavaScript is initialized with onload="selectRadioButton()" in the body tag.


Submission instructions: Submit assignments via the Canvas course management system. Submit the full URL for each exercise in the assignment, listing the URLs in the same order that they are listed in the assignment. To minimize typos in URLs it is strongly recommended that you copy the URLs from the address bar of the browser rather than trying to type them. Incorrect URLs will not be graded and no credit will be given.

When pages are connected via navigation (as in your Bookstore project) it is only necessary to submit the URL of the first page.

Regular Expressions HTML Color Names Color Picker ASCII Character Codes Unicode Character Codes