1 / 13

Web Database Programming Week 6

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

Download Presentation

Web Database Programming Week 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Database Programming Week 6 Using Templates & Updating Web Database

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

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

  4. Template • Define HTML presentation • Placeholders • To be filled with data from PHP code • Extensions to PHP • Smarty • PEAR Integrated Template

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

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

  7. 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();

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

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

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

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

  12. Using SQL for Update • SQL UPDATE tablename SET columnname = value, ... WHERE condition • Notes • value are set on all records matching the condition!

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

More Related