0% found this document useful (0 votes)
38 views43 pages

Chapter 20

This document describes the directory structure and files used to build a database-driven web site for an online guitar shop. Key points: 1) Include files are used to include common elements like headers, footers, and navigation across site pages for consistency. 2) The tags.php file contains a function that takes user-entered text and converts it to valid HTML by wrapping paragraphs and list items in appropriate tags. 3) View files generate the output displayed on each page by querying the database, passing data to templates, and including common elements from other files. 4) A consistent directory structure separates files by type (views, includes, images) for organization.

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views43 pages

Chapter 20

This document describes the directory structure and files used to build a database-driven web site for an online guitar shop. Key points: 1) Include files are used to include common elements like headers, footers, and navigation across site pages for consistency. 2) The tags.php file contains a function that takes user-entered text and converts it to valid HTML by wrapping paragraphs and list items in appropriate tags. 3) View files generate the output displayed on each page by querying the database, passing data to templates, and including common elements from other files. 4) A consistent directory structure separates files by type (views, includes, images) for organization.

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

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:

* Thicker bridge block


* 3-ply parchment pick guard
* Tinted neck

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) {

// 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);

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);

$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;
}
?>

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'];

// Get the application path


$uri = $_SERVER['REQUEST_URI'];
$dirs = explode('/', $uri);
$app_path = '/' . $dirs[1] . '/' . $dirs[2] . '/';

// Set the include path


set_include_path($doc_root . $app_path);
?>

$_SERVER is an array containing information


such as headers, paths, and script locations. The
entries in this array are created by the web
server.

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">

<!-- 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>
<div id="page">
<div id="header">
<h1>My Guitar Shop</h1>
</div>
<div id="main">

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' .
'&amp;category_id=' .
$category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
<li>&nbsp;</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' .
'&amp;category_id=' .
$category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
<li>&nbsp;</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'];

// Add HMTL tags to the description


$description = add_tags($description);

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);

// 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;
?>

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;
}

// 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;

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();

// Get product data


$product_id = $_GET['product_id'];
$product = get_product($product_id);

// 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&amp;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);

// display product list


include('product_list.php');
break;

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);

// Display Product List page for current category


header("Location: .?category_id=$category_id");
break;

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>

<!-- display product -->


<?php include '../../view/product.php'; ?>

<!-- display buttons -->


<div id="buttons">
<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>

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; ?>

<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 = '';
}
?>

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>&nbsp;</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

You might also like