ADVANCED WEB TECHNOLOGY
Chapter 3. Master MySQL Programming
Chapter 3. Master SQL Programming
Content
3.1. How to design a database
3.2. How to using SQL to create a MySQL database
3.3. How to using SQL to work with a MySQL database
3.4. Professional PHP for working with MySQL
3.5. A database-driven website
C1, Slide 2
3.5. A database-driven website
Objectives
Applied
1. Develop database-driven web sites using any of the skills in this
chapter or this section.
Knowledge
1. Describe the use of a content management system for a database-
driven application.
2. Describe the use of include files and the include path.
3. Describe the directory structure for a database-driven web site.
C20, Slide 3
The text that’s entered by the user
The Fender Stratocaster is <i>the</i> electric
guitar design that changed the world. This guitar
features a thicker bridge block for increased
sustain and a more stable point of contact
with the strings.
Features:
* Thicker bridge block
* 3-ply parchment pick guard
* Tinted neck
C20, Slide 4
The HTML that’s generated by the system
<p>The Fender Stratocaster is <i>the</i> electric
guitar design that changed the world. This guitar
features a thicker bridge block for increased
sustain and a more stable point of contact
with the strings.</p>
<p>Features:</p>
<ul>
<li>Thicker bridge block</li>
<li>3-ply parchment pick guard</li>
<li>Tinted neck</li>
</ul>
C20, Slide 5
The rules for this content management system
Use two returns to start a new paragraph.
Use an asterisk to mark items in a bulleted list.
Use one return between items in a bulleted list.
Use standard HMTL tags for bold and italics.
C20, Slide 6
The util/tags.php file
<?php
function add_tags($text) {
// Convert return characters to Unix new lines
// Convert Windows characters
$text = str_replace("\r\n", "\n", $text);
// Convert Mac characters
$text = str_replace("\r", "\n", $text);
// Get an array of paragraphs
$paragraphs = explode("\n\n", $text);
C20, Slide 7
The util/tags.php file (continued)
// Add tags to each paragraph
$text = '';
foreach($paragraphs as $p) {
$p = ltrim($p);
$first_char = substr($p, 0, 1);
if ($first_char == '*') {
// Add <ul> and <li> tags
$p = '<ul>' . $p . '</li></ul>';
$p = str_replace("*", '<li>', $p);
$p = str_replace("\n", '</li>', $p);
} else {
// Add <p> tags
$p = '<p>' . $p . '</p>';
}
$text .= $p;
}
return $text;
}
?>
C20, Slide 8
Code that uses the add_tags() function
$description_tags = add_tags($description);
C20, Slide 9
The home page for the Guitar Shop website
C20, Slide 10
The directory structure for the website
starting from htdocs/book_apps
C20, Slide 11
Files in the application’s root directory
index.php
home_view.php
main.css
C20, Slide 12
The util/main.php file
<?php
// Get the document root
$doc_root = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');
// Get the application path
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI');
$dirs = explode('/', $uri);
$app_path = '/' . $dirs[1] . '/' . $dirs[2] . '/';
// Set the include path
set_include_path($doc_root . $app_path);
?>
C20, Slide 13
The view/header.php file
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css"
href="<?php echo $app_path ?>main.css" />
</head>
<!-- the body section -->
<body>
<header>
<h1>My Guitar Shop</h1>
</header>
<main>
C20, Slide 14
The view/sidebar.php file
<aside>
<!-- These links are for testing only.
Remove them from a production application. -->
<h2>Links</h2>
<ul>
<li>
<a href="<?php echo $app_path; ?>">Home</a>
</li>
<li>
<a href="<?php echo $app_path . 'admin';
?>">Admin</a>
</li>
</ul>
C20, Slide 15
The view/sidebar.php file (continued)
<h2>Categories</h2>
<ul>
<!-- display links for all categories -->
<?php foreach ($categories as $category) : ?>
<li>
<a href="<?php echo $app_path . 'catalog' .
'?action=list_products' .
'&category_id=' .
$category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
<li> </li>
</ul>
</aside>
C20, Slide 16
The view/sidebar_admin.php file
<aside>
<h2>Links</h2>
<ul>
<li>
<a href="<?php echo $app_path; ?>">Home</a>
</li>
<li>
<a href="<?php echo $app_path .
'admin';?>">Admin</a>
</li>
</ul>
C20, Slide 17
The view/sidebar_admin.php file (continued)
<h2>Categories</h2>
<ul>
<!-- display links for all categories -->
<?php foreach ($categories as $category) : ?>
<li>
<a href="<?php echo $app_path .
'admin/product' .
'?action=list_products' .
'&category_id=' .
$category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</aside>
C20, Slide 18
The view/product.php file
<?php
// Parse data
$category_id = $product['categoryID'];
$product_code = $product['productCode'];
$product_name = $product['productName'];
$description = $product['description'];
$list_price = $product['listPrice'];
$discount_percent = $product['discountPercent'];
// Add HMTL tags to the description
$description_tags = add_tags($description);
C20, Slide 19
The view/product.php file (continued)
// Calculate discounts
$discount_amount = round(
$list_price * ($discount_percent / 100), 2);
$unit_price = $list_price - $discount_amount;
// Format discounts
$discount_percent_f = number_format(
$discount_percent, 0);
$discount_amount_f = number_format(
$discount_amount, 2);
$unit_price_f = number_format($unit_price, 2);
// Get image URL and alternate text
$image_filename = $product_code . '_m.png';
$image_path = $app_path . 'images/' . $image_filename;
$image_alt = 'Image filename: ' . $image_filename;
?>
C20, Slide 20
The view/product_php file (continued)
<h1><?php echo $product_name; ?></h1>
<div id="left_column">
<p><img src="<?php echo $image_path; ?>"
alt="<?php echo $image_alt; ?>"></p>
</div>
<div id="right_column">
<p><b>List Price:</b>
<?php echo '$' . $list_price; ?></p>
<p><b>Discount:</b>
<?php echo $discount_percent_f . '%'; ?></p>
<p><b>Your Price:</b>
<?php echo '$' . $unit_price_f; ?>
(You save <?php echo '$' . $discount_amount_f;
?>)</p>
C20, Slide 21
The view/product.php file (continued)
<form action="<?php echo $app_path . 'cart' ?>"
method="post">
<input type="hidden" name="action" value="add">
<input type="hidden" name="product_id"
value="<?php echo $product_id; ?>">
<b>Quantity:</b>
<input type="text" name="quantity" value="1"
size="2">
<input type="submit" value="Add to Cart">
</form>
<h2 class="no_bottom_margin">Description</h2>
<?php echo $description_tags; ?>
</div>
C20, Slide 22
The Product List page
C20, Slide 23
The Product View page
C20, Slide 24
The catalog/index.php file
<?php
require_once('../util/main.php');
require_once('../util/tags.php');
require_once('../model/database.php');
require_once('../model/product_db.php');
require_once('../model/category_db.php');
$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action == NULL) {
$action = 'list_products';
}
}
C20, Slide 25
The catalog/index.php file (continued)
switch ($action) {
case 'list_products':
// get current category
$category_id = filter_input(INPUT_GET,
'category_id', FILTER_VALIDATE_INT);
if ($category_id == NULL ||
$category_id === FALSE) {
$category_id = 1;
}
// get categories and products
$current_category = get_category($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
// display view
include('product_list.php');
break;
C20, Slide 26
The catalog/index.php file (continued)
case 'view_product':
$categories = get_categories();
// get product data
$product_id = filter_input(INPUT_GET, 'product_id',
FILTER_VALIDATE_INT);
$product = get_product($product_id);
// display product
include('product_view.php');
break;
}
?>
C20, Slide 27
The catalog/product_list.php fle
<?php include '../view/header.php'; ?>
<?php include '../view/sidebar.php'; ?>
<section>
<h1><?php echo $current_category['categoryName'];
?></h1>
<?php if (count($products) == 0) : ?>
<ul><li>There are no products in this
category.</li></ul>
<?php else: ?>
<ul>
<?php foreach ($products as $product) : ?>
<li>
<a href="?action=view_product&product_id=<?php
echo $product['productID']; ?>">
<?php echo $product['productName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</section>
<?php include '../view/footer.php'; ?>
C20, Slide 28
The catalog/product_view.php file
<?php include '../view/header.php'; ?>
<?php include '../view/sidebar.php'; ?>
<section>
<!-- display product -->
<?php include '../view/product.php'; ?>
</section>
<?php include '../view/footer.php'; ?>
C20, Slide 29
The Product View page
C20, Slide 30
The Product Add/Edit page
C20, Slide 31
The admin/product/index.php file
<?php
require_once('../../util/main.php');
require_once('../../util/tags.php');
require_once('../../model/database.php');
require_once('../../model/product_db.php');
require_once('../../model/category_db.php');
$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action == NULL) {
$action = 'list_products';
}
}
C20, Slide 32
The admin/product/index.php file (continued)
switch ($action) {
case 'list_products':
$category_id = filter_input(INPUT_GET,
'category_id', FILTER_VALIDATE_INT);
if ($category_id === FALSE) {
$category_id = 1;
}
$current_category = get_category($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
include('product_list.php');
break;
C20, Slide 33
The admin/product/index.php file (continued)
case 'view_product':
$categories = get_categories();
$product_id = filter_input(INPUT_GET, 'product_id',
FILTER_VALIDATE_INT);
$product = get_product($product_id);
include('product_view.php');
break;
case 'delete_product':
$product_id = filter_input(INPUT_POST, 'product_id',
FILTER_VALIDATE_INT);
$category_id = filter_input(INPUT_POST,
'category_id', FILTER_VALIDATE_INT);
delete_product($product_id);
// display product list for the current category
header("Location: .?category_id=$category_id");
break;
C20, Slide 34
The admin/product/index.php file (continued)
case 'show_add_edit_form':
$product_id = filter_input(INPUT_GET, 'product_id',
FILTER_VALIDATE_INT);
if ($product_id == NULL) {
$product_id = filter_input(INPUT_POST,
'product_id', FILTER_VALIDATE_INT);
}
$product = get_product($product_id);
$categories = get_categories();
include('product_add_edit.php');
break;
C20, Slide 35
The admin/product/index.php file (continued)
case 'add_product':
$category_id = filter_input(INPUT_POST,
'category_id', FILTER_VALIDATE_INT);
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$description = filter_input(INPUT_POST,
'description');
$price = filter_input(INPUT_POST, 'price',
FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST,
'discount_percent');
C20, Slide 36
The admin/product/index.php file (continued)
if ($category_id === FALSE ||
$code == NULL || $name == NULL ||
$description == NULL ||
$price === FALSE ||
$discount_percent === FALSE) {
$error = 'Invalid product data.
Check all fields and try again.';
include('../../errors/error.php');
} else {
$categories = get_categories();
$product_id = add_product($category_id, $code,
$name, $description, $price,
$discount_percent);
$product = get_product($product_id);
include('product_view.php');
}
break;
C20, Slide 37
The admin/product/index.php file (continued)
case 'update_product':
$product_id = filter_input(INPUT_POST, 'product_id',
FILTER_VALIDATE_INT);
$category_id = filter_input(INPUT_POST,
'category_id', FILTER_VALIDATE_INT);
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$description = filter_input(INPUT_POST,
'description');
$price = filter_input(INPUT_POST, 'price',
FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST,
'discount_percent');
C20, Slide 38
The admin/product/index.php file (continued)
if ($product_id === FALSE || $category_id === FALSE ||
$code === NULL || $name === NULL ||
$description === NULL ||
$price === FALSE ||
$discount_percent === FALSE) {
$error = 'Invalid product data.
Check all fields and try again.';
include('../../errors/error.php');
} else {
$categories = get_categories();
update_product($product_id, $code, $name,
$description,$price, $discount_percent,
$category_id);
$product = get_product($product_id);
include('product_view.php');
}
break;
}
?>
C20, Slide 39
The admin/product/product_view.php file
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<section>
<h1>Product Manager - View Product</h1>
<!-- display product -->
<?php include '../../view/product.php'; ?>
<!-- display buttons -->
<div class="last_paragraph">
<form action="." method="post" id="edit_button_form">
<input type="hidden" name="action"
value="show_add_edit_form"/>
<input type="hidden" name="product_id"
value="<?php
echo $product['productID'];?>" />
<input type="hidden" name="category_id"
value="<?php
echo $product['categoryID'];?>" />
<input type="submit" value="Edit Product" />
</form>
C20, Slide 40
The admin/product/product_view.php file
(cont.)
<form action="." method="post" >
<input type="hidden" name="action"
value="delete_product"/>
<input type="hidden" name="product_id"
value="<?php
echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php
echo $product['categoryID']; ?>" />
<input type="submit" value="Delete Product"/>
</form>
</div>
</section>
<?php include '../../view/footer.php';
C20, Slide 41
The admin/product/product_add_edit.php file
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<?php
if (isset($product_id)) {
$heading_text = 'Edit Product';
} else {
$heading_text = 'Add Product';
}
?>
<section>
<h1>Product Manager - <?php echo $heading_text; ?></h1>
<form action="index.php" method="post"
id="add_edit_product_form">
<?php if (isset($product_id)) : ?>
<input type="hidden" name="action"
value="update_product" />
<input type="hidden" name="product_id"
value="<?php echo $product_id; ?>" />
C20, Slide 42
The product_add_edit.php file (continued)
<?php else: ?>
<input type="hidden" name="action"
value="add_product" />
<?php endif; ?>
<input type="hidden" name="category_id"
value="<?php
echo $product['categoryID']; ?>" />
<label>Category:</label>
<select name="category_id">
<?php foreach ($categories as $category) :
if ($category['categoryID'] ==
$product['categoryID']) {
$selected = 'selected';
} else {
$selected = '';
}
?>
C20, Slide 43
The product_add_edit.php file (continued)
<option value="<?php
echo $category['categoryID']; ?>"
<?php echo $selected ?>>
<?php echo $category['categoryName']; ?>
</option>
<?php endforeach; ?>
</select><br>
<label>Code:</label>
<input type="text" name="code"
value="<?php echo htmlspecialchars(
$product['productCode']); ?>"><br>
<label>Name:</label>
<input type="text" name="name"
value="<?php echo htmlspecialchars(
$product['productName']); ?>"><br>
C20, Slide 44
The product_add_edit.php file (continued)
<label>List Price:</label>
<input type="text" name="price"
value="<?php echo $product['listPrice']; ?>"><br>
<label>Discount Percent:</label>
<input type="text" name="discount_percent"
value="<?php echo
$product['discountPercent']; ?>"><br>
<label>Description:</label>
<textarea name="description"
rows="10"><?php echo htmlspecialchars(
$product['description']); ?>
</textarea><br>
<label> </label>
<input type="submit" value="Submit">
</form>
C20, Slide 45
The product_add_edit.php file (continued)
<div id="formatting_directions">
<h2>How to format the Description entry</h2>
<ul>
<li>Use two returns to start a new
paragraph.</li>
<li>Use an asterisk to mark items in a bulleted
list.</li>
<li>Use one return between items in a bulleted
list.</li>
<li>Use standard HMTL tags for bold and
italics.</li>
</ul>
</div>
</section>
<?php include '../../view/footer.php'; ?>
C20, Slide 46