Home arrow PHP arrow Page 5 - PHP Fast Template

Multiple templates: Rows of data into a table - 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
Perhaps the data needed for the web page is a table of records from a database. The best way to present it would be through a table. With FastTemplate this can be accomplished cleanly. Look at this variation on the previous example to see how its done.

<?php //getData.php include "class.FastTemplate.php3"; //instantiate a new FastTemplate instance $tpl = new FastTemplate("../templates"); //associate three HTML templates to a variables //toplevel is the parent document, table is parent to the rows template, //rows contains the data $tpl->define(array( "toplevel" => "phone_numbers.tpl" "table" => "table.tpl", "rows" => "rows.tpl" )); //Connect to database mysql_connect (localhost, root, passwd); //select database to use mysql_select_db (testDB); //select entire set of records from MySQL database for example code $GetData = mysql_query ("select Phone from Business"); //Get array results if ($GetDataArray = mysql_fetch_array($GetData)) { do { //Associate contents of hash array with variable $Phone = $GetDataArray["Phone"]; //assign $Phone to FastTemplate instance $tpl->assign("PHONE", $Phone); //concatenate each row of data as the do //loop cycles through the array of records $tpl->parse(ROWS, ".rows"); }while ($GetDataArray = mysql_fetch_array($GetData)); //when all the records have been obtained //and parsed through the ROWS template, //parse the entire table $tpl->parse(PHONERECORDS, "table"); //pass the data to the HTML template $tpl->parse(MAIN, "toplevel"); $tpl->FastPrint(); } //if no records found, print error message else { print("Error obtaining data."); } ?>
In this example, the data for PHONE will be parsed through the ROWS (rows.tpl) template which looks like this:

<TR> <TD> {PHONE} </TD> </TR>
Each row will be connected to the end of the previous one. Once they have all been parsed, the entire set will be parsed as whole and placed in the TABLE (table.tpl) template, which looks like this:

<TABLE> {ROWS} </TABLE>
Finally, the entire table of records will parsed through the parent template which looks like this:

<HTML> <BODY> <P> Record of phone numbers: {PHONERECORDS} </P> </BODY> </HTML>
Now the entire set of data has been parsed and displayed to the screen cleanly formatted and completely separate from logic. At this point, designers can edit the template files for different fonts, images, text, etc. and leave the code alone.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: