用PHP5进行三层开发

它还会为user表模式生成example.ini配置文件:

[user]
user_Id = 129
first_Name = 130
last_Name = 130
email = 130

[user__keys]
user_Id = N

Smarty文件

现在是创建几个Smarty文件的时候了:

>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

此脚本实例化了一个新Smarty对象.设置Smarty属性.

dataobjects
|---- cache
|---- configs
|---- core
|---- plugins
|---- templates
|---- templates_c
 
11/10/2004  11:17 a.m.    <DIR> .
11/10/2004  11:17 a.m.    <DIR> ..
11/10/2004  11:17 a.m.    <DIR> cache
11/10/2004  11:17 a.m.    <DIR> configs
11/10/2004  11:17 a.m.    <DIR> core
11/10/2004  11:17 a.m.    <DIR> plugins
11/10/2004  11:17 a.m.    <DIR> templates
11/10/2004  11:17 a.m.    <DIR> 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

给Smarty模板分配变量.

< br/>configDB.php
<?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;
}
?>

添加将在insert.tpl 使用的变量.调用模板insert.tpl .

<?php
 
/**
 * Table Definition for user
 */
require_once 'DB/DataObject.php';
 
class DataObjects_User extends DB_DataObject 
{
    //START_AUTOCODE
 
    /* the code below is auto generated do not remove the above tag */
    var $__table = 'user';       // table name
    var $user_Id;                // int(11)  not_null primary_key auto_increment
    var $first_Name;             // string(30)  not_null
    var $last_Name;              // string(40)  not_null
    var $email;                  // string(100)  not_null
 
    /* Static get */
    function staticGet($k,$v=NULL) {
  return DB_DataObject::staticGet('DataObjects_User',$k,$v);
 }
 
    /* the code above is auto generated do not remove the tag below */
    //END_AUTOCODE
}
?>

This script saves data by using a PEAR::DataObject 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 out an update.

view.php

  <? 
   require_once('DB/DataObject.php');
   require('configDB.php');
   require("include.php");
   $user = DB_DataObject::factory('user');
   $user->find();
   while ($user->fetch()) {
      $smarty->append('users', array(
          'ID'        => $user->user_Id,
          'FIRSTNAME' => $user->first_Name,     
          'LASTNAME'  => $user->last_Name,     
          'EMAIL'     => $user->email,     
       ));
    }
   $smarty->assign('TITLE','List Users');
  $smarty->assign('HEADER','List User');
  $smarty->assign('data0','User_Id');
  $smarty->assign('data1','First Name');
  $smarty->assign('data2','Last Name');
  $smarty->assign('data3','email');
  $smarty->display('view.tpl');
  ?>

此脚本显示所有存储在user表中的数据.它加载 PEAR::DataObject 和include.php文件(给smarty模板分配变量).
第5行创建一个user对象的工厂.第6行执行find()方法.SELECT * FROM user从数据库中检索出了数据,通

过fetch()方法为模板保存数据,一次返回一条记录.
9 到14行是分配其他的变量给Smarty.

这些文件都应当放在 dataobjects目录中.

对于模板,这里有 index.tpl,list.tpl,和save.tpl.这里是他们的代码:

 //index.tpl
  <html>
   <head>
    <title>{$TITLE}</title>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   </head>
   <table align="center">    
    <tr> 
     <td>
       <b>{$HEADER}</b>
    </td>
   </tr>
  </table>    
  <table width="250" border="1" align="center" >
   <tr> 
     <td align="center">
       <input type="button" name="insert" value="Insert"    
           onclick="javascript:location.href='insert.php';">
    </td>
   </tr>
   <tr> 
     <td align="center">
     <input type="button" name="view" value="View" 
          onclick="javascript:location.href='view.php';">
   </td>
   </tr>
  </table>
  </body>
 </html>

站点主页,它在的3行和第9行分别显示$TITLE 和$HEADER,这些变量值是从index.php传递过来的.

这个脚本在web 浏览器上生成两个按钮,Insert和View,他们有相应的行为.如果用户点击Insert,系统将调

用 Insert.php.如果用户点击View,那么view.php将被调用

insert.tpl

 <html>
  <head>
    <title>{$TITLE}</title>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
     <form name="form1" action="save.php" method="post">
       <table width="300" border="1" align="center" >
       <tr>
          <td align="center">
            <b>{$HEADER}</b>
         </td>
       </tr>
       <tr>
         <td>
           {$data1}
            <input type="text" name="x">
         </td>
       </tr>
       <tr>
         <td>
           {$data2}
            <input type="text" name="y">
         </td>
       </tr>
       <tr>
         <td>
           {$data3}
           <input type="text" name="z">
         </td>
       </tr>
       <tr>
          <td align="center">
            <input type="submit" name="Submit" value="Add">
            <input type="button" name="Reset" value="Return/Cancel"
                 onclick="javascript:location.href='index.php';">
          </td>
        </tr>
      </table>
    </form>
  </body>
 </html>

这个模板有一个表单和两个按钮,Add 和Return/Cancel.

用户输入数据,first name,last name 和电子邮件字段.insert.php期望在名为x,y,z的变量中接收这些信息,用户点

击Add按钮将运行save.php.如果用户点击Return/Cancel,将会执行index.php.

view.tpl

 <html>
  <head>
    <title>{$TITLE}</title>
  </head>
  <body>
   <table align="center">
      <tr>
        <td align="center">
         <b>{$HEADER}</b>
       </td>
     </tr>
     </table>
   <table width="500" border="1" align="center">
  <tr>
        <td align="center">
         <b>{$data0}</b>
      </td>
        <td align="center">
         <b>{$data1}</b>
       </td>
        <td align="center">
         <b>{$data2}</b>
      </td>
     <td align="center">
         <b>{$data3}</b>
       </td>
     </tr>
  {section name=display loop=$users}
     <tr>
       <td>
         {$users[display].ID}
       </td>
       <td>
         {$users[display].FIRSTNAME}
       </td>
       <td>
         {$users[display].LASTNAME}
       </td>
       <td>
         {$users[display].EMAIL}
       </td>
     </tr>
     {/section}
     <br>
   </table>
   <br>
    <table align="center">
     <tr>
        <td align="center">
          <input name="vol" type="button" value="Return"  
               onclick="javascript:location.href='index.php';">
       </td>
     </tr>
   </table>
  </body>
 </html>

这个模板显示所有存储在example数据库中的所有数据.

最后,Return按钮把用户带回到主页.

所有的这些(*.tpl)文件必须放在templates目录下.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据