Question
Exercise 4-1 Enhance the Product Manager application This exercise has you enhance the Product Manager application by adding a page that lets you add and
Exercise 4-1 Enhance the Product Manager application
This exercise has you enhance the Product Manager application by adding a page that lets you add and delete categories. Test the Product Manager application
1. Start the Chrome browser and run the application in the ex_startsch04_ex1 directory. To do that, you can use this URL: http://localhost/ex_starts/ch04_ex1/ This should display the products for the first category in the database named my_guitar_shop1.
2. View the products in each of the categories.
3. Add a new product to the database. When you add this product, make sure to enter valid values for a product. Then, delete the product you just added from the database.
4. Click on the List Categories link at the bottom of the page. Note that this link leads to a page that’s under construction. However, the link back to the Product List page does work.
Add a Category List page In the rest of this exercise, you’ll add a page that looks like this:
5. Open the category_list.php file that’s in the directory for this application. It contains some of the headings and a link back to the Product List page.
6. In the category_list.php file, write the code that creates the category table shown above with all of the category names in the first column and Delete buttons in the second column, similar to how the index.php file works. Then, test that this table is displayed correctly.
7. In the category_list.php file, write the code that lets the user add a category to the database. This code should consist of a form that accepts the name for a new category followed by a Submit button that displays “Add”, similar to how the add_product_form.php file works. Then, test that this form is displayed correctly.
8. Create an add_category.php file that adds a category to the database and a delete_category.php file that deletes a category from the database. These files should display the Category List page after they add or delete a category, similar to how the add_product.php and delete_product.php files work.
9. Test the application by adding two categories. Then, navigate to the Product List page and note that the list of categories includes the new categories. Next, navigate to the Add Product page and note that the drop-down list includes the new categories.
10. Test the application by deleting the categories that you just added. However, don’t delete any of the existing categories because that will lead to products without categories. If necessary, though, you can restore the database by running the create_db.sql script again as described in the appendixes.
index.php:
require_once('database.php');
// Get category ID
if (!isset($category_id)) {
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE) {
$category_id = 1;
}
}
// Get name for selected category
$queryCategory = 'SELECT * FROM categories
WHERE categoryID = :category_id';
$statement1 = $db->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();
// Get products for selected category
$queryProducts = 'SELECT * FROM products
WHERE categoryID = :category_id
ORDER BY productID';
$statement3 = $db->prepare($queryProducts);
$statement3->bindValue(':category_id', $category_id);
$statement3->execute();
$products = $statement3->fetchAll();
$statement3->closeCursor();
?>
Product Manager
Product List
Add Product List CategoriesCode Name Price
category_list.php:
require_once('database.php');
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();
?>
Product Manager
Category List
Name | |||
---|---|---|---|
echo $category['categoryName']; ? | |||
Add Category
List Products
My Guitar Shop localhost/ex_solutions/ch04_ex1_sol/category_list.php Product Manager Category List Name Guitars Delete Basses Delete Drums Delete Add Category Name: Add List Products X 2017 My Guitar Shop, Inc.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To enhance the Product Manager application by adding edit functionalities for both PRODUCTS and CATEGORIES you will need to follow the instructions and tasks outlined Below is a stepbystep guide to as...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started