Php template technical example

Source: Internet
Author: User
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 ?? ---- Databas php template technology 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, 'freshlove@gmail.com');INSERT INTO `test` VALUES (2, 'dinahani', 'female', 29, 'dinahani@gmail.com');INSERT INTO `test` VALUES (3, 'sam', 'male', 1, 'sam@gmail.com');INSERT INTO `test` VALUES (4, 'girl', 'female', 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); } 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   
 
 {All_lists} 
   
Members List
Name Sex Age Email
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']); $ row = @ mysql_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

?

?

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.