Home arrow PHP arrow Page 4 - PHP Fast Template

MySQL Data parsed through FastTemplates - PHP

Why separate presentation from logic?The simple answer to the question is, "It keeps things simple". If presentation wasn't separated from logic around your house, you'd have to be an electrician to replace light-switch covers. The same goes for large-scale web applications. Graphic designers shouldn't need to be software engineers in order to update the fonts in a web page. Separating logic from presentation makes that possible.

TABLE OF CONTENTS:
  1. PHP Fast Template
  2. Assigning templates to objects
  3. Assigning variables to FastTemplate objects
  4. MySQL Data parsed through FastTemplates
  5. Multiple templates: Rows of data into a table
  6. Summary
By: Ian Felton
Rating: starstarstarstarstar / 27
July 02, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Using the above information as a guide, look at this code that works on data taken from a MySQL database and parses it through HTML templates.

<?php //getData.php include "class.FastTemplate.php3"; //instantiate a new FastTemplate instance $tpl = new FastTemplate("../templates"); //associate an HTML template to a variable $tpl->define(array( "toplevel" => "phone_numbers.tpl" )); //Connect to database mysql_connect (localhost, root, passwd); //select database to use mysql_select_db (testDB); //select data from MySQL database for example code $GetData = mysql_query ("select Phone from Business where Name = '$Name'"); //Get array results $GetDataArray = mysql_fetch_array($GetData); //Associate contents of hash array with variable $Phone = $GetDataArray["Phone"]; //assign $Phone to FastTemplate instance $tpl->assign("PHONE", $Phone); //pass the data to the HTML template $tpl->parse(MAIN, "toplevel"); $tpl->FastPrint(); ?>
From here, the phone number data taken from the database will be passed to the HTML template, "phone_numbers.tpl". Here are the contents of phone_numbers.tpl :

<HTML> <BODY> <P> Here is the phone number you wanted: {PHONE} </P> </BODY> </HTML>
On the web page it will look like this:

Here is the phone number you wanted: 555-5555




 
 
>>> More PHP Articles          >>> More By Ian Felton
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: