PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Three-Tier Development with PHP 5
Pages: 1, 2

Smarty files

It's time to create several files for Smarty:

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 for the application.

index.php
1 <? 
2  require("include.php");
3  $smarty->assign('TITLE','ACCESS MySQL DATABASE IN THREE TIERS WITH PHP');
4  $smarty->assign('HEADER','WHAT WISH DO ?');
5  $smarty->display('index.tpl');
?>

This script shows how to assign values to variables with Smarty. It calls include.php on line 2.

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'); 
?>

Much like the previous script, this adds variables that will appear in the insert.tpl template.

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  ?>

This script displays all of the data stored in the user table. It loads PEAR::DataObject and include.php (to assign variables of Smarty) that to be shown in the template view.tpl. Line 5 creates a factory of user objects. Line 6 issues a find, which is equivalent to SELECT * FROM user. After selecting the data, it's time to store it for the template via fetch(). This returns one record at a time, which the code stores in a template variable named users, append()ing each row, named by the keys ID, FIRSTNAME, LASTNAME, and EMAIL.

Lines 9 through 14 assign values to other Smarty variables.

All of these .php files should go in the dataobjects directory.

Now for the templates, there are index.tpl, list.tpl, and save.tpl. Here is their code:

index.tpl
1  <html>
2   <head>
3    <title>{$TITLE}</title>
4     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5   </head>
6   <table align="center">    
7    <tr> 
8     <td>
9       <b>{$HEADER}</b>
10    </td>
11   </tr>
12  </table>    
13  <table width="250" border="1" align="center" >
14   <tr> 
16     <td align="center">
17       <input type="button" name="insert" value="Insert"    
           onclick="javascript:location.href='insert.php';">
18    </td>
19   </tr>
20   <tr> 
21     <td align="center">
22      <input type="button" name="view" value="View" 
          onclick="javascript:location.href='view.php';">
23   </td>
24   </tr>
25  </table>
26  </body>
27 </html>

This template is the main page of the system. It shows two variables, $TITLE and $HEADER, in lines 3 and 9, respectively. These values of the variables come from index.php.

Later comes a table with two buttons, Insert and View, which have those respective actions. If the user clicks on Insert, the server invokes insert.php (see line 17). If the user clicks on View, this calls view.php (see lines 3 and 4 of that script).

insert.tpl
1 <html>
2  <head>
3    <title>{$TITLE}</title>
4     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5  </head>
6  <body>
7     <form name="form1" action="save.php" method="post">
8       <table width="300" border="1" align="center" >
9        <tr>
10          <td align="center">
11            <b>{$HEADER}</b>
12         </td>
13       </tr>
14       <tr>
15         <td>
16           {$data1}
17            <input type="text" name="x">
18         </td>
19       </tr>
20       <tr>
21         <td>
22           {$data2}
23            <input type="text" name="y">
24         </td>
25       </tr>
26       <tr>
27         <td>
28           {$data3}
29            <input type="text" name="z">
30         </td>
31       </tr>
32       <tr>
33          <td align="center">
34            <input type="submit" name="Submit" value="Add">
35            <input type="button" name="Reset" value="Return/Cancel"
                 onclick="javascript:location.href='index.php';">
36          </td>
37        </tr>
38      </table>
39    </form>
40  </body>
41 </html>

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

The user enters data for the first name, last name, and email fields (lines 16, 22, and 28). insert.php expects to receive this information in variables named x, y, and z (lines 17, 23, and 29). Clicking on the Add button calls save.php (see line 7) to perform the save. In the contrary case, if the user clicks on Return/Cancel, it executes index.php (line 35).

view.tpl
1 <html>
2  <head>
3    <title>{$TITLE}</title>
4  </head>
5  <body>
6   <table align="center">
7      <tr>
8         <td align="center">
9         <b>{$HEADER}</b>
10       </td>
11     </tr>
12     </table>
13   <table width="500" border="1" align="center">
14   <tr>
16        <td align="center">
17         <b>{$data0}</b>
18       </td>
19        <td align="center">
20         <b>{$data1}</b>
21       </td>
22        <td align="center">
23         <b>{$data2}</b>
24       </td>
25     <td align="center">
26         <b>{$data3}</b>
27       </td>
28     </tr>
29  {section name=display loop=$users}
30     <tr>
31       <td>
32         {$users[display].ID}
33       </td>
34       <td>
35         {$users[display].FIRSTNAME}
36       </td>
37       <td>
38         {$users[display].LASTNAME}
39       </td>
40       <td>
41         {$users[display].EMAIL}
42       </td>
43     </tr>
44     {/section}
45     <br>
46   </table>
47   <br>
48    <table align="center">
49     <tr>
50        <td align="center">
51          <input name="vol" type="button" value="Return"  
               onclick="javascript:location.href='index.php';">
52       </td>
53     </tr>
54   </table>
55  </body>
56 </html>

This template is able to display all of the data stored in the database example.

The first table shows the HEADER of the table, and the second shows data with its respective header (see lines 17, 20, 23, and 26, with the values coming from view.php). Line 29 shows the extraction of data by looping over the users array from view.php to show the values of the user, ID, FIRSTNAME, LASTNAME, and EMAIL fields (lines 32, 35, 38, and 41).

Finally, the Return button sends users back to the home page.

All of the template files (*.tpl) must go in the templates directory.

Conclusion

Working in three tiers with PHP saves repetitive work and makes for maintainable code, partly due to object oriented programming. It's easy to use the powerful Smarty and PEAR::DB_DataObject together.

Luis Yordano Cruz is a systems engineer in Trujillo PERU with more than five years of experience developing web-based applications with PHP, Java/J2EE, and JavaScript.


Return to the PHP DevCenter.


Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: