DekGenius.com
Previous Section  < Day Day Up >  Next Section

C.10 Chapter 11

C.10.1 Exercise 1:

$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What's For Dinner</title>
  <link>http://menu.example.com/</link>
  <description>These are your choices of what to eat tonight.</description>
  <item>
   <title>Braised Sea Cucumber</title>
   <link>http://menu.example.com/dishes.php?dish=cuke</link>
   <description>Gentle flavors of the sea that nourish and refresh you.</description>
  </item>
  <item>
   <title>Baked Giblets with Salt</title>
   <link>http://menu.example.com/dishes.php?dish=giblets</link>
   <description>Rich giblet flavor infused with salt and spice.</description>
  </item>
  <item>
   <title>Abalone with Marrow and Duck Feet</title>
   <link>http://menu.example.com/dishes.php?dish=abalone</link>
   <description>There's no mistaking the special pleasure of abalone.</description>
  </item>
 </channel>
</rss>
_XML_;
$xml = simplexml_load_string($menu);
print "<ul>\n";
foreach ($xml->channel->item as $item) {
    print '<li><a href="' . $item->link .'">' . $item->title ."</a></li>\n";
}
print '</ul>';

C.10.2 Exercise 2:

<?php
  // Load form helper functions
require 'formhelpers.php';
if ($_POST['_submit_check']) {
    // If validate_form( ) returns errors, pass them to show_form( )
    if ($form_errors = validate_form( )) {
        show_form($form_errors);
    } else {
        // The submitted data is valid, so process it
        process_form( );
    }
} else {
    // The form wasn't submitted, so display
    show_form( );
}
function show_form($errors = '') {
    if ($errors)) {
        print 'You need to correct the following errors: <ul><li>';
        print implode('</li><li>',$errors);
        print '</li></ul>';
    }
    // the beginning of the form
    print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
    // title
    print 'Title: ';
    input_text('title', $_POST);
    print '<br/>';
    // link
    print 'Link: ';
    input_text('link', $_POST);
    print '<br/>';
    // description
    print 'Description: ';
    input_text('description', $_POST);
    print '<br/>';
    // the submit button
    input_submit('submit','Generate Feed');
    // the hidden _submit_check variable and the end of the form
    print '<input type="hidden" name="_submit_check" value="1"/>';
    print '</form>';
}
function validate_form( ) {
    $errors = array( );
    // title is required
    if (! strlen(trim($_POST['title']))) {
        $errors[  ] = 'Enter an item title.';
    }
    // link is required
    if (! strlen(trim($_POST['link']))) {
        $errors[  ] = 'Enter an item link.';
    // It's tricky to perfectly validate a URL, but we can
    // at least check to make sure it begins with the right 
    // string
    } elseif (! (substr($_POST['link'], 0, 7) =  = 'http://') ||
              (substr($_POST['link'], 0, 8) =  = 'https://')) {
        $errors[  ] = 'Enter a valid http or https URL.';
    }
    
    // description is required
    if (! strlen(trim($_POST['description']))) {
        $errors[  ] = 'Enter an item description.';
    }
    return $errors;
}
function process_form( ) {
    // Send the Content-Type header
    header('Content-Type: text/xml');
    // print out the beginning of the XML, including the channel information
    print<<<_XML_
<rss version="0.91">
 <channel>
  <title>What's For Dinner</title>
  <link>http://menu.example.com/</link>
  <description>This is your choice of what to eat tonight.</description>
   <item>
_XML_;
  
    // print out the submitted form data
    print '  <title>' . htmlentities($_POST['title']) . "</title>\n";
    print '  <link>' . htmlentities($_POST['link']) . "</link>\n";
    print '  <description>' . htmlentities($_POST['description']) . 
"</description>\n";
   
    // print out the end of the XML
    print<<<_XML_
  </item>
 </channel>
</rss>
_XML_;
}
?>

C.10.3 Exercise 3:

<?php
require 'DB.php';
require 'formhelpers.php'; // load the form element printing functions
$db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant');
if (DB::isError($db)) { die("Can't connect: " . $db->getMessage( )); }
$db->setErrorHandling(PEAR_ERROR_DIE);
$db->setFetchMode(DB_FETCHMODE_ASSOC);
if ($_POST['_submit_check']) {
    if ($form_errors = validate_form( )) {
        show_form($form_errors);
    } else {
        process_form( );
    }
} else {
    show_form( );
}
function show_form($errors = '') {
    if ($errors) {
        print 'You need to correct the following errors: <ul><li>';
        print implode('</li><li>',$errors);
        print '</li></ul>';
    }
    // the beginning of the form
    print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
    print '<table>';
    // the price
    print '<tr><td>Price:</td><td>';
    input_text('price', $_POST);
    print '</td></tr>';
    
    // form end
    print '<tr><td colspan="2"><input type="submit" value="Search Dishes">';
    print '</td></tr>';
    print '</table>';
    print '<input type="hidden" name="_submit_check" value="1"/>';
    print '</form>';
}      
function validate_form( ) {
    $errors = array( );
    if (! strval(floatval($_POST['price'])) == $_POST['price']) {
        $errors[  ] = 'Please enter a valid price.';
    } elseif ($_POST['price'] <= 0) {
        $errors[  ] = 'Please enter a price greater than 0.';
    }
    return $errors;
}
function process_form( ) {
    global $db;
    header('Content-Type: text/xml');
    $dishes = $db->getAll('SELECT dish_name, price FROM dishes WHERE price >= ?',
                          array($_POST['price']));
    print "<dishes>\n";
    foreach ($dishes as $dish) {
        print " <dish>\n";
        print '  <name>'  . htmlentities($dish['dish_name']) . "</name>\n";
        print '  <price>' . htmlentities($dish['price']) . "</price>\n";
        print " </dish>\n";
    }
    print '</dishes>';
}
?>

C.10.4 Exercise 4:

<?php
require 'formhelpers.php';
if ($_POST['_submit_check']) {
    if ($form_errors = validate_form( )) {
        show_form($form_errors);
    } else {
        process_form( );
    }
} else {
    show_form( );
}
function show_form($errors = '') {
    if ($errors) {
        print 'You need to correct the following errors: <ul><li>';
        print implode('</li><li>',$errors);
        print '</li></ul>';
    }
    // the beginning of the form
    print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
    print '<table>';
    // the search term
    print '<tr><td>Search Term:</td><td>';
    input_text('term', $_POST);
    print '</td></tr>';
    
    // form end
    print '<tr><td colspan="2"><input type="submit" value="Search News Feed">';
    print '</td></tr>';
    print '</table>';
    print '<input type="hidden" name="_submit_check" value="1"/>';
    print '</form>';
}      
function validate_form( ) {
    $errors = array( );
    if (! strlen(trim($_POST['term']))) {
        $errors[  ] = 'Please enter a search term.';
    }
    return $errors;
}
function process_form( ) {
    
    // Retrieve the news feed
    $feed = simplexml_load_file('http://rss.news.yahoo.com/rss/topstories');
    if ($feed) {
        print "<ul>\n";
        foreach ($feed->channel->item as $item) {
            if (stristr($item->title, $_POST['term'])) {
                print '<li><a href="' . $item->link .'">' ;
                print htmlentities($item->title);
                print "</a></li>\n";
            }
        }
        print '</ul>';
    } else {
        print "Couldn't retrieve feed.";
    }
}
?>

    Previous Section  < Day Day Up >  Next Section