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

A Complete Data Retrieval Form

Example 7-56 is another complete database and form program. It presents a search form and then prints an HTML table of all rows in the dishes table that match the search criteria. Like Example 7-30, it relies on the form helper functions being defined in a separate formhelpers.php file.

Example 7-56. Form for searching the dishes table

<?php // Load PEAR DB require 'DB.php'; // Load the form helper functions. require 'formhelpers.php'; // Connect to the database $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage( )); } // Set up automatic error handling $db->setErrorHandling(PEAR_ERROR_DIE); // Set up fetch mode: rows as objects $db->setFetchMode(DB_FETCHMODE_OBJECT); // Choices for the "spicy" menu in the form $spicy_choices = array('no','yes','either'); // The main page logic: // - If the form is submitted, validate and then process or redisplay // - If it's not submitted, display 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 the form is submitted, get defaults from submitted parameters if ($_POST['_submit_check']) { $defaults = $_POST; } else { // Otherwise, set our own defaults ...

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