Chapter 20
Chapter 20
Chapter 20
A database-driven
web site
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 1
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.
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 2
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:
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 3
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>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 4
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.
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 5
The util/tags.php file
<?php
function add_tags($text) {
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 6
The util/tags.php file (continued)
// Add tags to each paragraph
$text = '';
foreach($paragraphs as $p) {
$p = ltrim($p);
return $text;
}
?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 7
Code that uses the add_tags function
$description = add_tags($description);
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 8
The Home page for the Guitar Shop web site
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 9
The directory structure for the web site
starting from htdocs/book_apps
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 10
Files in the application’s root directory
index.php
home_view.php
main.css
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 11
The util/main.php file (an include file)
<?php
// Get the document root
$doc_root = $_SERVER['DOCUMENT_ROOT'];
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 12
The view/header.php file (an include file)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ... >
<html xmlns="http://www.w3.org/1999/xhtml">
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 13
The view/sidebar.php file (an include file)
<div id="sidebar">
<ul>
<!-- These links are for testing only.
Remove them from a production application. -->
<h2>Links</h2>
<li>
<a href="<?php echo $app_path; ?>">Home</a>
</li>
<li>
<a href=
"<?php echo $app_path; ?>admin">Admin</a>
</li>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 14
The view/sidebar.php file (continued)
<h2>Categories</h2>
<!-- 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>
</div>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 15
The view/sidebar_admin.php file (an include file)
<div id="sidebar">
<ul>
<h2>Links</h2>
<li>
<a href="<?php echo $app_path; ?>">Home</a>
</li>
<li>
<a href="<?php echo $app_path; ?>
admin">Admin</a>
</li>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 16
The view/sidebar_admin.php file (continued)
<h2>Categories</h2>
<!-- 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; ?>
<li> </li>
</ul>
</div>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 17
The view/product.php file (an include 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'];
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 18
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 = number_format($discount_percent, 0);
$discount_amount = number_format($discount_amount, 2);
$unit_price = number_format($unit_price, 2);
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 19
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>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 20
The view/product.php file (continued)
<div id="right_column">
<p><b>List Price:</b>
<?php echo '$' . $list_price; ?></p>
<p><b>Discount:</b>
<?php echo $discount_percent . '%'; ?></p>
<p><b>Your Price:</b>
<?php echo '$' . $unit_price; ?>
(You save <?php echo '$' . $discount_amount; ?>)</p>
<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>Description</h2>
<?php echo $description; ?>
</div>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 21
The Product List page
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 22
The Product View page
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 23
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');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 24
The catalog/index.php file (continued)
switch ($action) {
case 'list_products':
// get current category
$category_id = $_GET['category_id'];
if (empty($category_id)) {
$category_id = 1;
}
// Display view
include('product_list.php');
break;
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 25
The catalog/index.php file (continued)
case 'view_product':
$categories = get_categories();
// Display product
include('product_view.php');
break;
}
?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 26
The catalog/product_list.php file
<?php include '../view/header.php'; ?>
<?php include '../view/sidebar.php'; ?>
<div id="content">
<h1><?php echo $current_category['categoryName']; ?>
</h1>
<?php if (count($products) == 0) : ?>
<p>There are no products in this category.</p>
<?php else: ?>
<?php foreach ($products as $product) : ?>
<p>
<a href=
"?action=view_product&product_id=
<?php echo $product['productID']; ?>">
<?php echo $product['productName']; ?>
</a>
</p>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php include '../view/footer.php'; ?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 27
The catalog/product_view.php file
<?php include '../view/header.php'; ?>
<?php include '../view/sidebar.php'; ?>
<div id="content">
<!-- display product -->
<?php include '../view/product.php'; ?>
</div>
<?php include '../view/footer.php'; ?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 28
The Product View page
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 29
The Product Add/Edit page
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 30
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');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 31
The admin/product/index.php file (continued)
$action = strtolower($action);
switch ($action) {
case 'list_products':
// get categories and products
$category_id = $_GET['category_id'];
if (empty($category_id)) {
$category_id = 1;
}
$current_category = get_category($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 32
The admin/product/index.php file (continued)
case 'view_product':
$categories = get_categories();
$product_id = $_GET['product_id'];
$product = get_product($product_id);
include('product_view.php');
break;
case 'delete_product':
$category_id = $_POST['category_id'];
$product_id = $_POST['product_id'];
delete_product($product_id);
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 33
The admin/product/index.php file (continued)
case 'show_add_edit_form':
if (isset($_GET['product_id'])) {
$product_id = $_GET['product_id'];
} else {
$product_id = $_POST['product_id'];
}
$product = get_product($product_id);
$categories = get_categories();
include('product_add_edit.php');
break;
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 34
The admin/product/index.php file (continued)
case 'add_product':
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$description = $_POST['description'];
$price = $_POST['price'];
$discount_percent = $_POST['discount_percent'];
if (empty($code) || empty($name)
|| empty($description) || empty($price) ) {
$error = 'Invalid product data.Check fields.';
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;
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 35
The admin/product/index.php file (continued)
case 'update_product':
$product_id = $_POST['product_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$description = $_POST['description'];
$price = $_POST['price'];
$discount_percent = $_POST['discount_percent'];
$category_id = $_POST['category_id'];
if (empty($code) || empty($name)
|| empty($description) || empty($price) ) {
$error = 'Invalid product data.Check fields.';
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; } ?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 36
The admin/product/product_view.php file
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Product Manager - View Product</h1>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 37
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>
</div>
<?php include '../../view/footer.php'; ?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 38
The admin/product/product_add_edit.php file
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<?php
if (isset($product_id)) {
$heading_text = 'Edit Product';
} else {
$heading_text = 'Add Product';
}
?>
<h1 class="top">
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; ?>" />
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 39
The product_add_edit.php file (continued)
<?php else: ?>
<input type="hidden" name="action"
value="add_product" />
<?php endif; ?>
<label>Category:</label>
<select name="category_id">
<?php foreach ($categories as $category) :
if ($category['categoryID'] ==
$product['categoryID']) {
$selected = 'selected';
} else {
$selected = '';
}
?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 40
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="input" name="code"
value="<?php echo $product['productCode']; ?>" />
<br />
<label>Name:</label>
<input type="input" name="name"
value="<?php echo $product['productName']; ?>" />
<br />
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 41
The product_add_edit.php file (continued)
<label>List Price:</label>
<input type="input" name="price"
value="<?php echo $product['listPrice']; ?>" />
<br />
<label>Discount Percent:</label>
<input type="input" name="discount_percent"
value=
"<?php echo $product['discountPercent']; ?>" />
<br />
<label>Description:</label>
<textarea name="description" rows="10">
<?php echo $product['description']; ?>
</textarea>
<br />
<label> </label>
<input type="submit" value="Submit" />
</form>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 42
The product_add_edit.php file (continued)
<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>
<?php include '../../view/footer.php'; ?>
Murach's PHP and MySQL, C20 © 2010, Mike Murach & Associates, Inc. Slide 43