PHP Template Technology Examples
1. Site structure
?
?
Site
┗includes
?? ┗class.inc
┣templates
?? ┗index.htm
?? ┣list.htm
?? ┗content.htm
┣index.php
┗content.php
?
2. Library structure
?
?
----Database: ' Test '----------------------------Table structure for table ' test '--CREATE Table ' test ' ( ' id ' SM Allint (3) NOT NULL auto_increment, ' name ' varchar (TEN) NOT null default "', ' sex ' enum (' Male ', ' female ') not null de Fault ' male ', ' age ' smallint (2) is not null default ' 0 ', ' email ' varchar (a) NOT null default ', PRIMARY key
(' id ')) engine=myisam DEFAULT Charset=utf8 auto_increment=5;----dumping data for table ' test '--INSERT into ' test ' VALUES (1, ' freshlove ', ' Male ', ' freshlove@gmail.com '); INSERT into ' Test ' VALUES (2, ' Dinahani ', ' Female ', Dinah Ani@gmail.com '); insert INTO ' Test ' values (3, ' Sam ', ' Male ', 1, ' sam@gmail.com '); insert INTO ' Test ' values (4, ' Girl ', ' FE Male ', 0, ' girl@gmail.com ');
?
?
3. Code
?
---------------class.inc File--------
?
?
host = $host; $this->user = $user; $this->pwd = $pwd; } function connect () {return mysql_connect ($this->host, $this->user, $this->pwd);}} Class template{Private $source _file, function Get_file ($filename) {$this->source_file = file_get_contents ($ filename); The function parse ($tags, $vals) {if (!is_array ($tags)) return preg_replace (' |{'). $tags. '}| ', $vals, $this->source_file); else{foreach ($tags as $row) $keys [] = ' |{'. $row. '}| '; Return Preg_replace ($keys, $vals, $this->source_file);
?
?
?----------------index.htm File-------------------
?
?
?
<title>Home Page</title>
Members List
Name |
Sex |
| Age
Email |
{all_lists}
Total: {total_numbers}, Display {per_numbers}/page |
{Page} |
?
?
------------------list.htm File-------------------
?
?
?
?
?
-------------------content.htm File-----------------------
?
?
?
Home Page
{Name} |
{Sex} |
{Age} |
{Email} |
Member infos
Name |
{Name} |
Sex |
{Sex} |
Age |
{Age} |
Email |
{Email} |
?
----------------index.php File--------------------------
?
?
Connect (); mysql_select_db (' Test ', $con); $limit = 20; Number of rows per page $p = ($_get[' P '])? $_get[' P ']: 1; Current page number/***** generate list start *****/$lists = '; $tmpl->get_file (' templates/list.htm '); $tags = Array (' mid ', ' name ', ' sex ', ' age ', ' email '); Should be in the same order as the table field $result = mysql_query (' SELECT * FROM test ORDER BY id DESC limit '. $p-1) * $limit. ', '. $limit); while ($row = Mysql_fetch_row ($result)) $lists. = $tmpl->parse ($tags, $row); /***** Build list complete, paging begins *****/$tmpl->get_file (' templates/index.htm '); $total = @mysql_result (mysql_query (' SELECT count (id) from test '), 0); Total number of records $ps = Ceil ($total/$limit); Total pages $pagination = ' first page '; if ($p >1) $pagination. = '; else $pagination. = '; $pagination. = ' Prev '; if ($p < $ps) $pagination. = '; else $pagination. = '; $pagination. = ' Next last Page '; /***** page completion, generate page start *****/$tags = Array (' all_lists ', ' total_numbers ', ' per_numbers ', ' page '); $vals = Array ($lists, $total, $limit, $pagination); echo $tmpl->parse ($tags, $vals); ? >
?
----------------content.php File---------------
?
?
Connect (); mysql_select_db (' Test ', $con); $tmpl->get_file (' templates/content.htm '); $rs = mysql_query (' select * from Test where id= '. $_get[' id '); $row = @mysql_fetch_row ($RS); unset ($row [0]); Remove extra fields that are read out of the table, align replacements, or list fields in the SELECT statement $tags = Array (' name ', ' sex ', ' age ', ' email ', ' back '); $row [] = ' back '; Echo $tmpl->parse ($tags, $row);?>
?
?
?
Finished
?
?
?