Php template technical example 1. site structure ?? Website endpoints Des ?? Extends class. inc extends templates ?? ┗Index.htm ?? Listen list.htm ?? Response content.htm response index. php response content. php? 2. database structure ?? ---- Database: 'test' -- php template technical example
1. site structure
?
?
Site
Regionregiondes
? ? Category class. inc
Templates
? ? ┗Index.htm
? ? ┣List.htm
? ? ┗Content.htm
Using index. php
Invalid content. php
?
2. database structure
?
?
-- -- Database: `test`-- -- ---------------------- -- Table structure for table `test`-- CREATE TABLE `test` ( `id` smallint(3) NOT NULL auto_increment, `name` varchar(10) NOT NULL default '', `sex` enum('male','female') NOT NULL default 'male', `age` smallint(2) NOT NULL default '0', `email` varchar(20) 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', 28, [email protected]');INSERT INTO `test` VALUES (2, 'dinahani', 'female', 29, [email protected]');INSERT INTO `test` VALUES (3, 'sam', 'male', 1, [email protected]');INSERT INTO `test` VALUES (4, 'girl', 'female', 0, [email protected]');
?
?
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); } 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 -------------------
?
?
?
Home Page
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} |
{Back}
?
---------------- Index. php file --------------------------
?
?
Connect (); mysql_select_db ('test', $ con); $ limit = 20; // The number of lines displayed on each page $ p = ($ _ GET ['P'])? $ _ GET ['P']: 1; // The current page number/***** starts to generate the list *****/$ lists = ''; $ tmpl-> get_file ('Templates/list.htm'); $ tags = array ('mid ', 'name', 'sex', 'age', 'Email '); // It 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);/***** the list is generated, start with pagination *****/$ 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 Page size $ pagination = 'first page'; if ($ p> 1) $ pagination. = ''; else $ pagination. = ''; $ pagination. = 'prev'; if ($ p <$ ps) $ pagination. = ''; else $ pagination. = ''; $ pagination. = 'next Last page';/***** the Page is displayed. the generated Page starts *****/$ 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']); [email protected] _ fetch_row ($ rs); unset ($ row [0]); // remove unnecessary fields read from the table, alignment replacement items, or list fields $ tags = array ('name', 'sex', 'age', 'Email ', 'back') in the SELECT statement '); $ row [] = 'back'; echo $ tmpl-> parse ($ tags, $ row);?>
?
?
?
Finished
?
?
?