Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment 2 COMP1230 (Advanced Web Programming) COMP1230 - Assignment Two Fall 2021: Due date: Nov 07 by 11:59 pm. The objective of the assignment is to use an associative array, iterations,...

1 answer below »
Assignment
2
COMP1230 (Advanced Web Programming)

COMP1230 - Assignment Two Fall 2021: Due date: Nov 07 by 11:59 pm.
The objective of the assignment is to use an associative a
ay, iterations, conditional statements
and user-defined functions. The data.inc.php (click to download) contains an associative
$inventory a
ay that contains several books' information, such as book id, title, author, etc.



You are required to create two sets of functions.

1. HTTP request related functions: these functions provide access to client request
information such as accessing HTTP parameter, client IP address, request method
(post/get), etc.

2. Inventory utility functions: these functions facilitate accessing inventory data (books)

List of HTTP request related functions:

1. Write a PHP function to receive two strings and returns a string (parameter value).
https:
comp1230.gblearn.com/2021/assignments/assignment2
Function Parameters:
• Name of an HTTP request parameter
• A default value to replace non-existing parameters. (Optional parameter)

Note: The function should handle both get and post requests. Therefore, this function
is called when a post or get request is made to the page.
• get is my function name.
• get(‘param_name’)
• get(‘param_name’,’default_value’)

Example:
HTTP Request: assignment2.php?id=10
HTTP Response: should return 10?>.
------
HTTP Request: assignment2.php?id=10
HTTP Response: returns 1234 because parameter isbn did not exist in GET or POST ?>
------
HTTP Request:
form method='post'>
form>
If user inputs 5 in above form and clicks on submit:
XXXXXXXXXXHTTP Response: should return 5 ?>.
2. Write a PHP function to return a string containing the following information in a tabular
format. Hint $_SERVER supper global.
• The request method is used to access the page.
• The timestamp of the start of the request.
• The address of the page (if any) which refe
ed the user agent to the cu
ent
page.
• The IP address from which the user is accessing the cu
ent page.
• The user agent
owser information.
Note:
• Call this function right after your first function, to display information about the
equest
• Make sure to style the table.
• Convert timestamp to readable time (as shown in example below)
Expected Output: (localhost) your Output will be different.


3. Write a PHP function to create and return an HTML dropdown menu. The function
should receive the following three parameters:
a. A string - name of the dropdown
. An a
ay - dropdown options
c. An optional string parameter to pass the selected option value to this function.
Example: If the function is called with following arguments:
_menu('category',[‘cat 1’, ‘cat 2’], ‘cat 2’);
Dropdown menu will have two items (cat 1 and cat 2), with cat 2 being the
selected item. Please note, the first letter of the items are changed to uppercase,
to create the value for output.



Inventory utility functions:
4. Write a function to return an a
ay of all the categories in $inventory.
• Function prototype: function_name (): return a
ay (categories)
• Sample return: ['Fiction’, ‘Biographies’]
• Call the function you creaed in previous step (3), by passing the returned a
ay
(categories) to display the categories in a dropdown menu in the form created in
step 1.
i.e: $categories = function_name ()
call your function above
echo _menu('category', $categories);
Will produce following dropdown:
5. Write a function to return all the books for the selected category.
• Function prototype: function_name (cat_name) : return a
ay (books)
• Sample return:

• Print the return value in a readable format.


6. Write a function to search for a book in $inventory by category name and book id.
• Function prototype: function_name (cat_name,book_id) : return a
ay (book)
• Validation requirements:
1. Return the following string "Cannot find the category category_name" if
the category does not exist.
2. Return the following string "Cannot find the book" if the book id does not
exist.
3. Sample return: function_name('Fiction’,1)

4. Check if the returned value is an a
ay, and pass the returned a
ay to the
fuction you will be writing next (in step 7), to print the book information
in a tabular format. If you are not planning to complete the next function,
just call your function, by passing a category and an id of a book in our
inventory and print the returned book a
ay in a readable format.
7. Write a function to print book information in tabular format.
• Function prototype: function_name ($book): prints book inforamtion.
• Book price: Print prices for all book types (Paper Back, Hard Cover and Kobo
eBook)
• Suppose the sale price is not zero print both original price (o_price) and sale
price (s_price). Review the following Output for style-related detail.
• Sample output:

Ru
ic:
• Function 1: 10 marks
• Function 2: 15 marks
• Function 3: 15 marks
• Function 4: 10 marks
• Function 5: 10 marks
• Function 6: 20 marks
• Function 7: 20 marks
➢ Overall functionality 50%
➢ Logic, efficiency, reusability 30%
➢ Naming convention, documentation (valuable comments included in code),
eadability (spacing/indentation) 20%

When you lose marks:

1. Create only one file named assignment2.php in addition to the data file provided.
2. Misspelled file name (i.e assingmnet2.php) (-2 mark deduction)
3. You must include the data.inc.php file in your assignment two file (You will receive a
grade of zero if this is ignored)
4. The Output of your assignment must be valid HTML5. (Add the validation script before
closing body tag). (Invalid HTML -10)
5. Make sure to include (You will receive a grade of zero if
this is ignored)
Submission Guidelines:
• Upload the two files (assignment2.php and data.inc.php) to the following directory in your
GBLearn account comp1230/assignments/assignment2/
• Login to my.gblearn.com and ONLY submit your assignment2.php
• Follow the naming convention and have meaningful names for your variables and function.
• Apply proper indentation and space before and after operators for readability.
• Removing unnecessary/unrelated code & comments.
• Add necessary comments to summarize your code or to explain the programmer's intent.
• Your work will not be marked if your GBLearn account is not setup
• Please Make sure to:
• Grant, both of your instructors, access to preview content of your GBLearn account, by
adding them to the instructors list on my.gblearn.com
• Create comp1230 directory structure (specified on Blackboard)
• Update your GBLearn profile page (public_html/index.html)
1. to include a relative link to the comp1230 directory in your account
2. to show a picture of yourself o to include a
ief Bio about yourself

comp1230-week025-1xof3o0w.ppt
2
WEEK
COMP1230
*
Advanced Web Programming
PHP/MYSQL
Objectives
1. Explain how PHP is embedded within an HTML document.
2. Distinguish between PHP statements and comments.
3. Describe these PHP data types: integer, double, Boolean, NULL and string.
4. Rules for creating a PHP variable name.
5. Declaring variable and assigning a value to it.
6. Review the use of superglobals $_GET and $_POST a
ays
7. Describe the use of echo statement
8. Describe the rules for evaluating an arithmetic expression, including order of precedence and the use of parentheses.
9. Review some of the built-in functions
10. Introduction to filtering & sanitizing data
11. Built-in functions that pass control
12. A
ays
A PHP file that includes HTML and embedded PHP
Murach's PHP and MySQL (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
C2, Slide *
© 2017, Mike Murach & Associates, Inc.
The PHP file displayed in a
owse
Murach's PHP and MySQL (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
C2, Slide *
© 2017, Mike Murach & Associates, Inc.
PHP code: comments and statements
?php
/*********************************************
* This program calculates the discount for a
* price that's entered by the use
********************************************
 

get the data from the form
$list_price = $_GET['list_price'];


calculate the discount
$discount_percent = .20;
20% discount
$discount_amount =
$list_price * $discount_percent;
$discount_price =
$list_price - $discount_amount;
?
Another way to code single-line comments
# calculate the discount
$discount_percent = .20; # 20% discount
Murach's PHP and MySQL (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
C2, Slide *
© 2017, Mike Murach & Associates, Inc.
Syntax rules
PHP statements end with a semicolon.
PHP ignores extra whitespace in statements.
Murach's PHP and MySQL (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
C2, Slide *
© 2017, Mike Murach & Associates, Inc.
PHP data types
intege
double
oolean
string
a
ay
Object
NULL
Murach's PHP and MySQL (3rd Ed.)
© 2017, Mike Murach & Associates, Inc.
C2, Slide *
© 2017, Mike Murach & Associates, Inc.
Integer values (whole numbers)
15
an intege
-21
a negative intege
Double values (numbers with decimal
Answered 1 days After Nov 02, 2021

Solution

Shubham Kumar answered on Nov 03 2021
114 Votes
assignment2/assignment2.php
?php show_source(__file__)?
?php
equire("data.inc.php");
HTTP Request Related Function :
function get($variable, $default='1234') {
if(isset($_GET[$variable]))
return $_GET[$variable];
else if(isset($_POST[$variable]))
return $_POST[$variable];
else
return $default;
}
function getRequestInformation() {

we get all the required information from the $_SERVER a
ay, and then display it in tabular format
echo "tbody>";
echo "td>Requested Method
td
td>".$_SERVER['REQUEST_METHOD']."
td
t
";
echo "td>Access Time
td
td>".date('m/d/Y H:i:s', $_SERVER['REQUEST_TIME'])."
td
t
";
echo "td>Refe
ed Url
td
td>".$_SERVER['HTTP_REFERER']."
td
t
";
echo "td>Client IP Address
td
td>".$_SERVER['REMOTE_ADDR']."
td
t
";
echo "td>User Agent
td
td>".$_SERVER['HTTP_USER_AGENT']."
td
t
";
echo "
tbody
table
";
}
function _menu($title, $values, $selected = "assignment") {

The return string stores the entire HTML Code for the drop down menu
$returnString = "
Copy and Paste Your Assignment Here