用PHP5进行三层开发

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

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

[user__keys]
user_Id = N

Smarty文件

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

//include.php
 <?
   require('Smarty.class.php');
   $smarty = new Smarty;
   $smarty->template_dir = 'templates/';
   $smarty->compile_dir  = 'templates_c/';
   $smarty->config_dir   = 'configs/';
   $smarty->cache_dir    =  'cache/';
?>

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

//index.php& lt;br/> <? 
  require("include.php");
  $smarty->assign('TITLE','ACCESS MySQL DATABASE IN THREE TIERS WITH PHP');
  $smarty->assign('HEADER','WHAT WISH DO ?');
  $smarty->display('index.tpl');
?>

给Smarty模板分配变量.

 //insert.php
  <? 
  require("include.php"); 
  $smarty->assign('TITLE','INSERT DATA'); 
  $smarty->assign('HEADER','Insert Data'); 
  $smarty->assign('data1','First Name');
  $smarty->assign('data2','Last Name');
  $smarty->assign('data3','email');
  $smarty->display('insert.tpl'); 
?>

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

//save.php
   <?
  require_once('DB/DataObject.php');
   require('configDB.php');
   $user = DB_DataObject::factory('user');
   $user->first_Name = $x;
   $user->last_Name  = $y;
   $user->email      = $z;
   $user_Id = $user->insert();
   $user->update();
  echo "<script>location.href='index.php'</script>";
 ?>

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目录下.

发表回复

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.