Omar Carrillo

Solutions

Assignment

Problem 1

Problem 2

Problem 3

Problem 4

Problem 5

Problem 6

Problem 7

Problem 8

Problem 9

Homework 3

  1. Write a function to print out an HTML <img> tag. The function should accept a mandatory argument of the image URL and optional arguments for alt text, height, and width.

  2. Modify the function in the previous exercise so that the filename only is passed to the function in the URL argument. Inside the function, prepend a global variable to the filename to make the full URL. For example, if you pass photo.png to the function, and the global variable contains /images/, then the src attribute of the printed <img> tag would be /images/photo.png. A function like this is an easy way to keep your image tags correct, even if the images move to a new path or a new server. Just change the global variable – for example, from /images/ to http://images.example.com/.

  3. What does the following code print out?

    $cash_on_hand = 31;
    $meal = 25;
    $tax = 10;
    $tip = 10;
    while(($cost = restaurant_check($meal,$tax,$tip)) < $cash_on_hand) {
    $tip++;
    print "I can afford a tip of $tip% ($cost)\n";
    }
    function restaurant_check($meal, $tax, $tip) {
    $tax_amount = $meal * ($tax / 100);
    $tip_amount = $meal * ($tip / 100);
    return $meal + $tax_amount + $tip_amount;
    }


  4. Web colors such as #ffffff and #cc3399 are made by concatenating the hexadecimal color values for red, green, and blue. Write a function that accepts decimal red, green, and blue arguments and returns a string containing the appropriate color for use in a web page. For example, if the arguments are 255, 0, and 255, then the returned string should be #ff00ff. You may find it helpful to use the builtin
    function dechex( ), which is documented at http:// www.php.net/dechex.

  5. What does $_POST look like when the following form is submitted with the third option in the Braised Noodles menu selected, the first and last options in the Sweet menu selected, and 4 entered into the text box?

    <form method="POST" action="order.php">
    Braised Noodles with: <select name="noodle">
    <option>crab meat</option>
    <option>mushroom</option>
    <option>barbecued pork</option>
    <option>shredded ginger and green onion</option>
    </select>
    <br/>
    Sweet: <select name="sweet[ ]" multiple>
    <option value="puff"> Sesame Seed Puff
    <option value="square"> Coconut Milk Gelatin Square
    <option value="cake"> Brown Sugar Cake
    <option value="ricemeat"> Sweet Rice and Meat
    </select>
    <br/>
    Sweet Quantity: <input type="text" name="sweet_q">
    <br/>
    <input type="submit" name="submit" value="Order">
    </form>


  6. Write a process_form( ) function that prints out all submitted form parameters and their values. You can assume that form parameters have only scalar values.

  7. Write a program that does basic arithmetic. Display a form with text box inputs for two operands and a <select> menu to choose an operation: addition, subtraction, multiplication, or division. Validate the inputs to make sure that they are numeric and appropriate for the chosen operation. The processing function should display the operands, operator, and the result. For example, if the operands are 4 and 2 and the operation is multiplication, the processing function should display something like "4 * 2 = 8".

  8. Write a program that displays, validates, and processes a form for entering information about a package to be shipped. The form should contain inputs for the from and to addresses for the package, dimensions of the package, and weight of the package. The validation should check (at least) that the package weighs no more than 150 pounds and that no dimension of the package is more than 36 inches. You can assume that the addresses entered on the form are both U.S. addresses, but you should check that a valid state and a ZIP Code with valid syntax are entered. The processing function in your program should print out the information about the package in an organized, formatted report.

  9. (EXTRA CREDIT) Modify your process_form( ) function from Exercise 6.2 so that it correctly handles submitted form parameters that have array values. Remember, those array values could themselves contain arrays.
CS 479 - PHP Programming - Summer 2009 - NMSU