Calling a php function and looping on return values
This is a feature to be used when the same treatment is to be applied with
an unknown number or return values of the same type. A typical example is the
result of a database query.
The format is
{ bz:list function="function_name" arg1="value1"
... }
treatment
{ bz:endlist}
The return value of the function used should be an array of hash tables.
Things may get clearer with the example below :
Imagine a mysql database containing some members' personal information, gathered
in fields such as 'name' 'city' 'state' 'phone'.
Then the following code prints all the information we have on the members of
a specific city :
<html>
<?php function test ($d) {
$db_handle=DB::connect("mysql://username:password@server/database");
return $db->getAll("select * from MEMBERS where city=".$d['city'],DB_FETCHMODE_ASSOC);
} ?>
<body>
Here are the members from Paris. <br /><br />
{ bz:list function="test" city="Paris"}
Name : { $name} <br />
City : { $city} State : { $state} <br />
Phone number : { $phone} <br />
{ bz:endlist}
</body>
</html>
Easy database access
The use of the 'function' argument is mostly for heavier treaments. For a mere
database query, one can use the 'query' argument facility of bz:list. The format
is then :
{ bz:list query="query" }
The previous example becomes :
<html>
<body>
Here are the members from Paris. <br /><br />
{ bz:list query="select * from MEMBERS where city='Paris'"}
Name : { $name} <br />
City : { $city} State : { $state} <br />
Phone number : { $phone} <br />
{ bz:endlist}
</body>
</html>
Which is a bit shorter. If the presentation needs to change to turn out into
a table with columns labeled with the fields, this becomes :
<html>
<body>
Here are the members from Paris. <br /><br />
<table>
<tr><td>Name</td><td>City</td><td>State</td><td>Phone
Number</td></tr>
{ bz:listquery="select * from MEMBERS where city='Paris' "}
<tr><td>{ $name}</td><td>{ $city}</td><td>{ $state}</td><td>{ $phone}</td></tr>
{ bz:endlist}
</table>
</body>
</html>
Easy, isn't it ? And there was no php written.
|