Example 3
Finally we will now use the parser together with FastSQL.
You will see how incredibly easy it can be to make a template-, database-driven site.
Program File # 1: <?php 
# 2:
require_once("../stp.inc");
# 3: require_once(
"../class_mysql.inc");
# 4:
define("G_HOSTNAME"$_SERVER['HTTP_HOST']);
# 5:
# 6:
$STP = new parser2;
# 7:
# 8:
$params['HEADLINE'] = "My address book";
# 9:
#10:
#11:
define("G_HOST""localhost");
#12:
define("G_DBUSER""MyUsername");
#13:
define("G_DBPWD""MyPassword");
#14:
define("G_NORMDB""MyDB");
#15:
$conn = new FastSQL;
#16:
#17:
$addresses $conn->query("select * from ADDRESSES order by NAME"G_NORMDB);
#18:
$params['ENTRIES'] = $addresses;
#19:
#20:
$STP->setParams($params);
#21:
$STP->setTemplate("example5.tmpl");
#22:
$STP->parseAndEcho();
#23:
?>
HTML template file # 0: <?
# 1:
<html>
# 2:   <
head>
# 3:   <
title>Example 2</title>
# 4:   </
head>
# 5:   
# 6:   <
body>
# 7:   <
h1 align="center"><#HEADLINE#></h1>
# 8:   
<center>
# 9:   <
table border="1" cellpadding="3" cellspacing="3">
#10:     <
tr>
#11:       <
th align="left">Name</th>
#12:       <
th align="left">Phone</th>
#13:     </
tr>
#14:   <
#FOR ENTRIES#>
#15:     
<tr>
#16:       <
td><#Name#></td>
#17:       
<td><#Phone#></td>
#18:     
</tr>
#19:   <
#/FOR ENTRIES#>
#20:   
</table>
#21:   </
center>
#22:   </
body>
#23: </
html>
You may have noticed that this template is identical to step 2, the only difference is the way we create our data array:
First, we include the FastSQL class file
require_once("./class_mysql.inc");
For FastSQL to work it needs some constants with your database settings:
define("G_HOST", "localhost");
define("G_USER", "MyUsername");
define("G_HOST", "MyPassword");
define("G_NORMDB", "MyDB");
and then create a new instance of FastSQL:
$conn = new FastSQL;
Now, using the query method of FastSQL we fetch an array with all addresses
$addresses = $conn->query("select * from ADDRESSES order by NAME", G_NORMDB);
This will resut in a multidimensional array holding all the data we need.
Now, to pass them to the parser all we have to do is write
$params['ENTRIES'] = $addresses;
Yes. It is that easy. The rest is exactly like in example 2 and the output looks the same as well
See the result

Note:
If you want to use try this example you have to set up the table and insert your mysql data in the file example5.php

My Sites