130 likes | 255 Vues
This guide focuses on using templates in PHP to enhance web database management and development. It emphasizes the importance of separating presentation from processing, making code easier to maintain and more reusable. By utilizing template systems like HTML_Template_IT, developers can create HTML presentations with placeholders to be filled with data from PHP. The document also covers essential database operations such as creating, updating, inserting, and deleting records, providing structured examples and SQL syntax for practical implementation. Learn how to improve your web application’s design and functionality.
E N D
Web Database Programming Week 6 Using Templates & Updating Web Database
PHP & HTML • PHP code can be inserted anywhere in HTML code • PHP code can output any HTML code • Tightly mixed code (e.g. our sample code) • Hard to read • Hard to maintain • What if you want to change the appearance of the page? • What if you decide to use a different algorithm?
Separate Presentation and Processing • An important user interface design principle • Easy to change interface or backend processing independently • Easier maintenance • Reusable code • How? • Using templates
Template • Define HTML presentation • Placeholders • To be filled with data from PHP code • Extensions to PHP • Smarty • PEAR Integrated Template
PEAR • PHP Extension and Application Repository • Includes many packages • DB, HTML_Template_IT, Authentication, Encryption, Graphics, XML, SOAP… • Core packages comes with PHP 4.3.0 later • Optional packages needs to be installed
HTML_Template_IT Template • http://pear.php.net/manual/en/package.html.html-template-it.php • Template Format: • Regular HTML • Placeholder {placeholder_name} • Block <!-- BEGIN block_name --> <!-- END block_name -->
HTML_Template_IT Class • Use the class require_once “HTML/Template/IT.php” • Create the object $template = new HTML_Template_IT(“template_dir”); • Call the methods $template->loadTemplatefile(“template_filename”, true, true); $template->setCurrentBlock(“block_name”); $template->setVariable(“placeholder_name”, data); $template->parseCurrentBlock(); $template->show();
Update Database • Table Operations • Create table structure • Change table structure • Delete table • Record Operations • Insert a record to a table • Update a record • Delete a record
Making an Insert Form • Example • Notes • Recall that PHP will convert form field names into variables in the action page • use input type=hidden to set predefined and previously fixed values (like foreign keys); and to carry values forward
Using SQL for Insert • SQL INSERT INTO tablename (columnnames) VALUES (values) • Note • value do not have to come from form – they could, for example, be computed, or taken from another query • Remember ‘single quote’ for text values and not for numbers
An Update Form • Example • Notes • note the addition of a record ID value (which we must get from somewhere!); tells us which record we are updating.
Using SQL for Update • SQL UPDATE tablename SET columnname = value, ... WHERE condition • Notes • value are set on all records matching the condition!
Deleting data from a table • The form for the SQL part of the code is: DELETE FROM tablename WHERE condition • Note: • all records matching the condition are deleted • if there is no where clause deletes all records in table