Recommended PHP Template Technology

Source: Internet
Author: User
Tags array copy count include connect php file php template
Template

Site structure
Code:
Site
┗includes
┗class.inc
┣templet
┗index.htm
┣list.htm
┗content.htm
┣index.php
┗content.php
Library structure
Code:
--Database: ' Test '
--The structure of the table ' test '
CREATE TABLE ' Test ' (
' ID ' smallint (3) not NULL auto_increment,
' Name ' varchar ' not NULL default ',
' Sex ' enum (' Male ', ' female ') not NULL default ' male ',
' Age ' smallint (2) Not NULL default ' 0 ',
' Email ' varchar not NULL default ',
PRIMARY KEY (' id ')
) Type=myisam auto_increment=1;
---------------class.inc File--------
[Copy this Code] code:<?php
Class mycon{
Private $myhost;
Private $myuser;
Private $mypwd;
function Mycon ($host = "localhost", $user = "root", $pwd = "") {
$this->myhost = $host;
$this->myuser = $user;
$this->mypwd = $pwd;
}
function Connect () {
Return mysql_connect ($this->myhost, $this->myuser, $this->mypwd);
}
}
Class templet{
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{
$an = count ($tags);
for ($i =0; $i < $an; $i + +) {
$tags [$i] = "| {". $tags [$i]."}| ";
}
Return Preg_replace ($tags, $vals, $this->source_file);
}
}
}
?>

----------------index.htm File-------------------
[Copy this Code] Code:<HEAD>
<TITLE> Home </TITLE>
</HEAD>
<body style= "font-size:12px" >
<table width= "100%" cellpadding= "0" cellspacing= "1" bgcolor= #000000 style= "font-size:12px" >
<caption> member List </caption>
<tr bgcolor= "#ffffff" align=center>
&LT;TD width=25%> name </TD>
&LT;TD width=25%> Sex </TD>
&LT;TD width=25%> Age </TD>
&LT;TD width=25%>email</td>
</TR>
{All Lists}
<tr bgcolor= "#ffffff" >
&LT;TD colspan=2> Total {total number of bars} records, display {per page bar/page </TD>
&LT;TD colspan=2 align=right>{Paging}</td>
</TR>
</TABLE>
</BODY>
</HTML>

------------------list.htm File-------------------
[Copy this Code] Code:<tr bgcolor= "#ffffff" align=center>
<td><a href= "content.php?id={member ID}" >{name}</a></td><td>{sex}</td><td>{Age }</td><td>{email}</td>
</TR>

-------------------content.htm File-----------------------
[Copy this Code] Code:<HEAD>
<TITLE> member Information </TITLE>
</HEAD>
<body style= "font-size:12px" >
<table width= "100%" cellpadding= "0" cellspacing= "1" bgcolor= #000000 style= "font-size:12px" >
<caption> member Information </caption>
<tr bgcolor= "#ffffff" >
&LT;TD width=60> name </td><td>{name}</td></tr>
<tr bgcolor= "#ffffff" >
<TD> Sex </td><td>{Sex}</td></tr>
<tr bgcolor= "#ffffff" >
<TD> Age </td><td>{Age}</td></tr>
<tr bgcolor= "#ffffff" >
<TD>email</TD><TD>{email}</TD></TR>
</TABLE>
</BODY>

----------------index.php File--------------------------
[Copy this Code] code:<?php
Include ("Includes/class.inc");
$tmpl =new Templet;
$mycon =new Mycon;
$con = $mycon->connect ();
mysql_select_db ("Test", $con);
$lim = 20; Show number of rows per page
$p = ($_get[p])?  $_GET[P]: 1; Current page number
/***** Build list Start *****/
$lists = "";
$tmpl->get_file ("templet/list.htm");
$tags = Array ("Member id", "name", "Gender", "age", "email"); Should be in the same order as a table field
$rs = mysql_query ("SELECT * from test ORDER BY id DESC limit".) ( $p-1) * $lim. ", $lim");
while ($row =mysql_fetch_row ($rs)) {
$lists. = $tmpl->parse ($tags, $row);
}
/***** Build list complete, paging start *****/
$tmpl->get_file ("templet/index.htm");
$rn = @mysql_result (mysql_query ("SELECT count (ID) from test"), 0); Total number of records
$ps = Ceil ($rn/$lim); Total pages
$pagination = "<a href= '? p=1 ' > Home </a>";
if ($p >1) $pagination. = "<a href= '? p=". ($p-1). "' > ";
else $pagination. = "<font color= ' #777777 ' >";
$pagination. = "Prev </font></a>";
if ($p < $ps) $pagination. = "<a href= ' p=". ($p + 1). "' > ";
else $pagination. = "<font color= ' #777777 ' >";
$pagination. = "Next page </font></a> <a href= '? p={$ps} ' > Last </a>";
/***** pagination complete, build page start *****/
$tags = Array ("All lists", "total number of bars", "per page number", "paging");
$vals = Array ($lists, $rn, $lim, $pagination);
echo $tmpl->parse ($tags, $vals);
?>

----------------content.php File---------------
[Copy this Code] code:<?php
Include ("Includes/class.inc");
$tmpl =new Templet;
$mycon =new Mycon;
$con = $mycon->connect ();
mysql_select_db ("Test", $con);
$tmpl->get_file ("templet/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 a SELECT statement
$tags = Array ("name", "Sex", "age", "email");
echo $tmpl->parse ($tags, $row);
?>



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.