forked from ezSQL/ezsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_sql_with_smarty.html
185 lines (132 loc) · 7.33 KB
/
ez_sql_with_smarty.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Making Smarty EZier.</title>
</head>
<body bgcolor="#ffffff">
<b>Making Smarty EZier.<br>
</b><br>If you have used Smarty for templating your websites then you already know what a great resource it is. If not, you are missing out. Go to Smarty.php.net and check it out.<br>
<br>
In this article I will explain how using Smarty with EzSQL by Justin Vincent (<a href="http://justinvincent.com/">justinvincent.com</a>) can make your life even “ezier”. My intent here is not to explain the in depth workings of Smarty or EzSQL but to show how the use of these two classes together is synergistic.<br>
<br>
First we’ll have quick look at EzSQL, then Smarty, then the two combined.<br>
<br>
<b>EzSQL:<br>
</b><br>
When getting data from a database using native php it might look something like this:<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>mysql_connect("localhost", "mysql_user", "mysql_password")<br>
or die("could not connect");<br>
mysql_select_db("mydb"); <br>
$result = mysql_query("SELECT id, name FROM mytable"); <br>
while ($row = mysql_fetch_array($result)) <br>
{ <br> printf ("ID: %s Name: %s", $row[0], $row["name"]);<br>
} <br>
mysql_free_result($result); <br>
</code></td>
</tr>
</table>
<br>
In the reality I think many of us now use a class of some kind so it would look a little more like this (Y.M.M.V.)<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>require '/path/to/myConnector.php';<br>
$db=new myConnector("localhost", "mysql_user", "mysql_password");<br>
<br>
$db->query("SELECT id, name FROM mytable");<br>
while ($db‡next_record()){<br>
printf ("ID: %s Name: %s", $db->f(‘id’), $db->f(‘name’);<br>
}<br>
</code></td>
</tr>
</table>
<br>
I think you’d agree that’s fewer lines and generally a better solution. Using a database class is great as it wraps the database, makes getting the data easier, but doesn’t cover the presentation aspect. That still has to be done by intermingling php and HTML<br>
<br>
EzSQL is only a little different in it’s set up, however the results are returned as an array as you can see here.<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>define("EZSQL_DB_USER", "mysql_user"); <br>
define("EZSQL_DB_PASSWORD", "mysql_password");<br>
define("EZSQL_DB_NAME", "my_db");<br>
define("EZSQL_DB_HOST", "localhost"); <br>
require ‘/path/to/ez_sql.php';<br>
<br>
$result_array = $db->get_results("SELECT id, name FROM mytable");<br>
foreach($result_array as $row_obj) {<br>
printf ("ID: %s Name: %s", $db->id, $db->name;<br>
}<br>
</code></td>
</tr>
</table>
<br>
<br>
<b>
Smarty: <br>
</b><br>
Next we’ll take a look at the Smarty process<br>
<br>
Smarty is a class. In simplistic terms it's usage is: <br>
- Instantiate a Smarty object<br>
- Push the data for the page into the Smarty object<br>
- Get Smarty to apply the template(s) to the data -- (“skin” the object so to speak)<br>
<br>
In code it looks like this:<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>include '/path/to/Smarty.php';<br>$Smarty=new Smarty;<br>$Smarty->assign('somevar', 'some data I want to display');<br>$Smarty->assign('some_db_results', $db->get_my_data());<br>$Smarty->display('name_of_template.tpl');<br>
</code></td>
</tr>
</table>
<br>
The template had entries for {$somevar} and {$some_db_results} so the assigned data is displayed inside the template at those points.<br>
<br>
You have probably already figured out the ending to this story but in case you haven’t, this is what happens when you put these two classes together.<br>
<br>
<b>Putting them together:<br>
<br>
</b>This is the code for both the php file and the template file. The synergy being that the results from EzSQL can be passed straight into Smarty and the layout is done there. This means less coding for the programmer and more flexibility for the designer.<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>define("EZSQL_DB_USER", "mysql_user"); <br>
define("EZSQL_DB_PASSWORD", "mysql_password");<br>
define("EZSQL_DB_NAME", "my_db");<br>
define("EZSQL_DB_HOST", "localhost"); <br>
require ‘/path/to/ez_sql.php'; <br>
// the $db object is instantiated by the php file<br>
<br>include '/path/to/Smarty.php';<br>$Smarty=new Smarty;<br>
<br>$Smarty->assign('DB_RESULTS', $db->get_results("SELECT id, name FROM mytable");<br>$Smarty->display('template.tpl');<br>
<br>
//template file template.tpl<br>
<br>
<FONT face="Courier New"><table border="0" cellspacing="0" cellpadding="3"><BR>
{foreach from=$DB_RESULTS <FONT
color=#ff0000>item="row_obj"</FONT>}<BR>
<tr><BR>
<td>ID: {$row_obj ->id}</td> <BR>
<td>Name: {$row_obj ->name}</td><BR>
</tr><BR>
{/foreach}<BR>
</table></FONT><br>
</code></td>
</tr>
</table>
<br>
Of course this is not a real world example. In the real world, at least in my real world, all the configuration is done in a “loader” file that takes care of all the constant definitions, data paths, instantiations and so on. This file is prepended in the httpd container for the domain or in .htaccess file so the process is automated. So, in reality the php file only contains the last two lines of the example.<br>
<br>
Since switching to this method of creating sites my workload has gotten lighter, my code is more readable and the number of line of code is far less. Even the design side is more fun as you can control the display in the presentation layer and not have to worry abobut tweaking the underlying PHP files. All in all faster and “ezier”... Try it.<br>
<br>
Happy coding,<br>
<br>
Steve Warwick Ph.D.<br>
<hr>
For information on using my modified version of Justin’s class ez_results with Smarty check out my article “EZ pagination For Smarty.”<br>
</body>
</html>