PHP Template Technology Examples

Source: Internet
Author: User
Tags php template
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>   
 
  Age 
    {all_lists} 
   
Members List
Name Sex 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}

?

----------------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); ? &GT  

?

----------------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

?

?

?

  • {Back}

    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.