给Smarty模板分配变量. 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'); ?> 添加将在insert.tpl 使用的变量.调用模板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(); 10 echo "<script>location.href='index.php'</script>"; 11 ?> 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 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'); 10 $smarty->assign('HEADER','List User'); 11 $smarty->assign('data0','User_Id'); 12 $smarty->assign('data1','First Name'); 13 $smarty->assign('data2','Last Name'); 14 $smarty->assign('data3','email'); 15 $smarty->display('view.tpl'); 16 ?> |