O'Reilly logo

Learning PHP 5 by David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7

Exercise 1:

<?php
require 'DB.php';
$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);
$dishes = $db->getAll('SELECT dish_name,price FROM dishes ORDER BY price');
if (count($dishes) > 0) {
    print '<ul>';
    foreach ($dishes as $dish) {
        print "<li> $dish[dish_name] ($dish[price])</li>";
    }
    print '</ul>';
} else {
    print 'No dishes available.';
}
?>

Exercise 2:

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required