Exercise 9
Automated Breadcrumb Trail Using PHP
Resources for this exercise:
- See the Course Schedule for due date. Exercises are due before the beginning of class on the due date.
- See the Homework Frequently Asked Questions (FAQ) page for answers to questions about homework exercises.
- See Self-Writing Breadcrumb Trail
- See Breadcrumbs Markup
In this exercise you will use a block of PHP code to make an automatically generated breadcrumb trail.
1) Go to:
http://www.drquincy.com/resources/code/php/selfwritingbreadcrumbtrail
and read about the breadcrumbtrail code.
2) But use my modified code instead. Copy this code. Place this block of code into an include file.
<?php
function getFilenameWithoutExt($filename) {
$pos = strpos($filename, '.');
if($pos === false){
return $filename;
}else{
return substr($filename, 0, $pos);
}
}
?>
<?php
function breadcrumbtrail() {
// title of the page
$titleofpage = getFilenameWithoutExt(basename($_SERVER['PHP_SELF']));
// file path
$path = $_SERVER[ 'PHP_SELF' ];
// split the path into folders
$path = explode( "/", $path );
// set the label of the first element to 'Home'
$path[ 0 ] = "Home";
// set the last element of the array to the title of the page
$path[ count( $path ) - 1 ] = $titleofpage;
$counter = 0;
// go through the array and output the trail
foreach( $path as $key=>$value ) {
// compose hyperlink for all breadcrumb trail parts except the last element
if( $counter < ( count( $path ) - 1 ) ) {
$href = "/";
for( $c = 1; $c <= $counter; $c++ ) {
$href .= $path[ $c ] . "/";
}
// split link name into words by capital letter
// regular expression provided by james pittendreigh
$words = preg_split( '/([A-Z][a-z]+)/', $value, -1, PREG_SPLIT_DELIM_CAPTURE );
$linkname = "";
$isfirst = true;
// add whitespace to the link name
foreach( $words as $v ) {
// only consider adding whitespace if the element is populated
if( !empty( $v ) ) {
// don't add whitespace to the first element
if( !$isfirst ) {
$linkname .= " ";
}
else {
$isfirst = false;
}
// capitalize directory names
$linkname .= ucwords($v);
}
}
// output the hyperlink
echo "<a href=\"$href\">$linkname</a> > ";
}
else {
// just output the value
echo ucwords($value);
}
$counter ++;
}
}
breadcrumbtrail();
?>
3) Create a Web page for exercise 9.
4) This page must contain a self-generating breadcrumb trail. The breadcrumb trail code must be in an include file. So place the breadcrumb trail code in an include file, and then use PHP to include the file containing the breadcrumbtrail into this exercise page.
5) The page must contain the XHTML 1.0 Strict DocType, as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
6) This page must contain links (either buttons or text links) to the HTML and CSS validators. See How to Put HTML and CSS Validate Buttons on Your Page for information on how I want you to to do this. (Use the block of code on this page to make the button links or text links, so that clicking on the links automatically lets you know if the page validates or not.)
7) This page must display your name and say "Exercise 9."
8) This page's <title> must indicate that this is your
exercise 9 page, so it should say something like "John Sky's DM160C Exercise
9".
9) This page must contain a relative link to your home page.
10) Make sure the HTML on this page validates (using the XHTML 1.0 Strict DocType) with no errors, according to the W3C's online HTML Validator at http://validator.w3.org/. You can easily do this by clicking on your HTML validator button. For more information, see How to Use the W3C HTML and CSS Validators for step-by-step directions.
11) If you have used any CSS, make sure the CSS used with this page validates, with no errors, according to the W3C's online CSS Validator at: http://jigsaw.w3.org/css-validator/validator-uri.html. You can easily do this by clicking on your CSS validator button. See the CSS section of How to Use the W3C HTML and CSS Validators for step-by-step directions.
12) If there are any errors, fix them, re-upload the page, and validate it again.
13) Make sure your home page has a working link to all your completed exercises, including this exercise, and that the HTML and CSS on your home page validates.
14) Submit the feedback form. To receive credit for this exercise you must submit thi feedback form before it expires. This exercise is due on 4/28/08. Allowing for a one-day grace period, the form will expire at the end of the day following the due date, which means the form for this exercise will expire at 12:00 AM on 04/30/08.
Thanks to Frank Turner for his modification to the code that capitalizes the directory names!