Three-layer development with PHP5 _php

Source: Internet
Author: User
Keywords Development conduct the to td td center
Tags pear php template table definition smarty template
Original: http://www.onlamp.com/pub/a/php/2004/12/09/three_tier.html
Three-tier Development with PHP 5
by Luis Yordano Cruz
12/09/2004

This article demonstrates the powerful features of PHP three-tier development, PEAR::D b_dataobject for business logic, smarty for display logic, where you are assumed to be familiar

Html,smarty,pear::D b_dataobject,mysql and PHP5. If you need additional knowledge, the following article explains some of the principles:

Simplifying business logic with PHP DataObject
Smarty Introduction: a PHP template engine
PHP Scalability: Myth

You should have installed and configured the Apache,mysql, and PHP5 (or Iis,mysql5 and PHP)

PEAR::D b_dataobject
PEAR::D B_dataobject is an abstract API for user database access. It is a SQL builder and data modeling layer based on PEAR::D B. It

Maps a database table to a PHP class and provides public SQL functions such as select,insert,update, and delete. This makes it not even

Developers of SQL can also write good database access code and encourage a clear separation of presentation logic and business logic.

(Db_oo has moved to the pear, now is db_dataobject, if you have old code to update, view about updating from old Db_oo code to

Db_dataobjects's comments).

Related reading

Upgrading to PHP 5
by Adam Trachtenberg

Table of Contents
Index
Sample Chapter

Read Online--safari Search on Safari:

Only this book all of Safari
Code Fragments only
DataObject performs the other tasks. First, it builds SQL statements based on the object ' s variables and the builder

Methods. Second, it acts as a datastore for a table row. There ' s a core class, which extend for each

Your tables So, you put the data logic inside the data classes. There ' s also an included Generator to make

Your configuration files and your base classes.

DataObject performs two tasks. First, it constructs SQL statements and builder methods based on object variables. Second, it is stored as a database table

Storage. Here is a core class that inherits it for each table so that you can put the data logic in the data class. This also includes a generator,


DataObject greatly simplifies database access code, making it easier to develop large, data-driven sites.

At present, Alan Knowles, the leads developer of PEAR::D B_dataobject, is working on a new project called Dbdo,

A C implementation of the PEAR package Db_dataobjects, based on Libgda. His goal are to create the next

Generation of PEAR::D b_dataobjects.

SMARTY

Smarty is a PHP template engine that separates content from a Web page presentation. It uses the GPL license.

Large projects commonly separate the role of the graphic designer from that of the programmer. However,

Programming in PHP have the tendency to combine those, a person and roles the code. This can bring

Difficulties when it comes time-to-change some part of the page ' s design. If the page mixes content and

Presentation, the developer have to crawl through the program to find the presentation. Smarty helps to solve

This problem.

Combining the
The first thing to does when starting this project was to create a workspace on which to store the project ' s code.

Then it's time to configure PEAR::D b_dataobject-Connect to the MySQL database MySQL (name:example),

Map the database tables to PHP classes, and then configure Smarty for the presentation tier for the user. Here

Is those steps in more detail:


Create a work environment

Create a directory called DataObjects.

To install pear from the command line::D b_dataobject, type:
>pear Install Date
>pear Install Db_dataobject
>pear List

Installed PACKAGES:
===================
Package VERSION State
Archive_tar 1.2 Stable
Console_getopt 1.2 Stable
DB 1.6.5 Stable
Db_dataobject 1.7.1 Stable * (Goal)
Date 1.4.3 Stable
Mail 1.1.3 Stable
NET_SMTP 1.2.6 Stable
Net_socket 1.0.2 Stable
PEAR 1.3.1 Stable
PHPUnit 1.0.1 Stable
Xml_parser 1.2.0 Stable
Xml_rpc 1.1.0 Stable


Installing and configuring Smarty


From the beginning of the download, (I use the 2.6.5 version of Smarty) to extract to your own directory. Smarty.class.php from its Libs directory,

Smarty_Compiler.class.php, Config_File.class.php, and Debug.tpl files are copied to the DataObjects directory.


Also copy the core and plugins directories and all of the content. Create several new directories named templates, Templates_c, Configs,

and cache.


The final dataobjects directory contains:


|----Cache
|----configs
|----Core
|----Plugins
|----templates
|----Templates_c

11/10/2004 11:17 a.m.

.
11/10/2004 11:17 a.m. ..
11/10/2004 11:17 a.m. Cache
11/10/2004 11:17 a.m. Configs
11/10/2004 11:17 a.m. Core
11/10/2004 11:17 a.m. Plugins
11/10/2004 11:17 a.m. Templates
11/10/2004 11:17 a.m. Templates_c
07/09/2004 09:48 a.m. 13,105 Config_File.class.php
16/04/2004 03:03 a.m. 5,117 Debug.tpl
10/09/2004 02:15 p.m. 65,350 Smarty.class.php
10/09/2004 07:14 p.m. 90,924 Smarty_Compiler.class.php
4 Archivos 174,496 bytes
8 dirs 6,699,454,464 bytes Libres


Create a database
Create a database named example, which contains a table named user, without worrying about the schema, which we will create later.


Configuration pear::D b_dataobject
To build the data object, create the following file

configdb.php
Require_once ' db/dataobject.php ';
$config = Parse_ini_file (' Example.ini ', TRUE);

foreach ($config as $class = = $values) {
$options = &pear::getstaticproperty ($class, ' options ');
$options = $values;
}
?>

This script creates a connection to the database based on the values in the example configuration file, as shown below.

Example.ini
[Db_dataobject]
Database = Mysql://root: @localhost/example
Schema_location =/dataobjects/schema/
Class_location =/dataobjects/
Require_prefix =/dataobjects/
Class_prefix = Dataobjects_
Extends_location = db/dataobject.php
extends = Db_dataobject


Automatically build the database schema

Contains two procedures to build an object-relational map of a database and automatically create a class from the example database User table. All fields in the table

The name becomes a class member variable.


Create the appropriate pattern:

C:\php\pear\db\dataobject>c:\php\php.exe createtables.php \
C:\dataobjects\example.ini


This will generate the user.php file:

/**
* Table Definition for user
* www.knowsky.com
*/
Require_once ' db/dataobject.php ';

Class Dataobjects_user extends Db_dataobject
{
# # #START_AUTOCODE

/* The code below is auto generated does not remove the above tag */
var $__table = ' user '; Table name
var $user _id; Int (one) not_null Primary_key auto_increment
var $first _name; String (not_null)
var $last _name; String (not_null)
var $email; String (not_null)

/* Static Get */
function Staticget ($k, $v =null) {
Return Db_dataobject::staticget (' Dataobjects_user ', $k, $v);
}

/* The code above is auto generated does not remove the tag below */
# # #END_AUTOCODE
}
?>

It also generates a Example.ini configuration file for the user table pattern:

[User]
USER_ID = 129
First_Name = 130
Last_Name = 130
email = 130

[User__keys]
user_id = N


Smarty files
It ' s time to create several files for Smarty:

Smarty File
Now is the time to create several smarty files:

include.php
1 2 require (' Smarty.class.php ');
3 $smarty = new Smarty;
4 $smarty->template_dir = ' templates/';
5 $smarty->compile_dir = ' templates_c/';
6 $smarty->config_dir = ' configs/';
7 $smarty->cache_dir = ' cache/';
?>

This script instantiates a new Smarty object. Sets the Smarty property.

index.php
1 2 require ("include.php");
3 $smarty->assign (' TITLE ', ' ACCESS MySQL DATABASE in three TIERS with PHP ');
4 $smarty->assign (' HEADER ', ' What's WISH do? ');
5 $smarty->display (' Index.tpl ');
?>

Assign variables to the Smarty template.

insert.php
1 2 require ("include.php");
3 $smarty->assign (' TITLE ', ' INSERT DATA ');
4 $smarty->assign (' HEADER ', ' Insert Data ');
5 $smarty->assign (' data1 ', ' first Name ');
6 $smarty->assign (' data2 ', ' last Name ');
7 $smarty->assign (' data3 ', ' email ');
8 $smarty->display (' Insert.tpl ');
?>

Add the variables that will be used in INSERT.TPL. Call Template insert.tpl.

save.php
1 2 require_once (' db/dataobject.php ');
3 require (' configdb.php ');
4 $user = db_dataobject::factory (' user ');
5 $user->first_name = $x;
6 $user->last_name = $y;
7 $user->email = $z;
8 $user _id = $user->insert ();
9 $user->update ();
echo "";
?>

This script saves data is using a PEAR::D ataobject for the user table. Line 2 Loads the class DataObject, and

Line 3 calls configdb.php to connect to the database. Line 4 Creates an instance of a user object (see user.php).

Lines 5 through 7 pass the variables collected from the form in Insert.tpl ($x, $y, and $z) in order to save the

Data in the database. The primary key of the table is an AutoIncrement column, so it doesn ' t need a value there.

Line 8 Inserts the object, and line 9 carries-an update.

view.php
1 2 require_once (' db/dataobject.php ');
3 require (' configdb.php ');
4 Require ("include.php");
5 $user = db_dataobject::factory (' user ');
6 $user->find ();
7 while ($user->fetch ()) {
8 $smarty->append (' Users ', array (
' ID ' = $user->user_id,
' FIRSTNAME ' = $user->first_name,
' LASTNAME ' = $user->last_name,
' EMAIL ' = $user->email,
));
}
9 $smarty->assign (' TITLE ', ' List Users ');
Ten $smarty->assign (' HEADER ', ' List User ');
One by one $smarty->assign (' data0 ', ' user_id ');
$smarty->assign (' data1 ', ' first Name ');
$smarty->assign (' data2 ', ' last Name ');
$smarty->assign (' data3 ', ' email ');
$smarty->display (' View.tpl ');
?>

This script displays all data stored in the user table. It loads pear::D ataobject and include.php files (assigning variables to smarty templates).
Line 5th creates a factory for the user object. Line 6th executes the Find () method. SELECT * FROM user retrieves data from the database,

The fetch () method saves the data for the template and returns one record at a time.

Rows 9 to 14 are assigned other variables to Smarty.


These files should all be placed in the DataObjects directory.


For templates, here are Index.tpl,list.tpl, and SAVE.TPL. Here's their code:

Index.tpl
1
2
3{$TITLE}
4
5
6





7 8 11 12

9 {$HEADER}
10

13






14 16 19 20 21st 24 25

17
onclick= "javascript:location.href= ' insert.php ';" >
18

22
onclick= "javascript:location.href= ' view.php ';" >
23

26
27


The site home page, which displays $title and $header in the 3 and 9th rows, and the values of these variables are passed from index.php.


This script generates two buttons on a Web browser, insert and view, and they behave accordingly. If the user clicks Insert, the system will

Use insert.php. If the user clicks View, then view.php will be called

Insert.tpl
1
2
3{$TITLE}
4
5
6
7
40
41


This template has a form and two buttons, Add and Return/cancel.


User input data, first name,last name and email fields. insert.php expects to receive this information in a variable named x, Y, z, and the user points

Click the Add button to run save.php. If the user clicks Return/cancel, the index.php will be executed.

View.tpl
1
2
3{$TITLE}
4
5
6





7 8 11 12

9 {$HEADER}
10

13
















14 16 19 22 25 28 {section Name=display loop= $users}30 31 34 37 40 43 {/SECTION}45 46

+ {$data 0}
18

{$data 1}
21st

{$data 2}
24

$ {$data 3}
27

{$users [Display].id}
33

{$users [display]. FIRSTNAME}
36

{$users [display]. LASTNAME}
39

$users [Display]. EMAIL}
42

47

48



49 50 53 54

51
onclick= "javascript:location.href= ' index.php ';" >
52

55
56

This template displays all the data stored in the example database.


Finally, the return button takes the user back to the home page.

All of these (*.TPL) files must be placed in the Templates directory.

  • Related Article

    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.