07-28-2008, 11:13 AM
|
#5 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
|
Quote:
Originally Posted by Sam Granger
Sorry for the old bump.
Want to output each row from a mysql while statement. Whats the best way to get around this?
Meant something so in the template I can add:
<% beginwhile:results %>
<div>
<h3><% Name %></h3>
<p><% Address %></p>
</div>
<% endwhile:results %>
|
I'd go for something like this:
PHP Code:
public function replace_while($name, Array $data)
{
if(strstr('<% beginwhile:' . strtolower($name) . ' %>', $this->template) && strstr('<% beginwhile:' . strtolower($name) . ' %>', $this->template))
{
foreach($data as $key => $value)
{
$this->replace_tags($key, $value);
}
$this->replace_tags('beginwhile:' . strtolower($name), '');
$this->replace_tags('endwhile:' . strtolower($name), '');
}
}
Then to call it you'd do something like:
PHP Code:
$results_d = $db->query('SELECT * FROM `results` WHERE `x` = 4 ORDER BY `the_moon`');
if($results_d->num_rows < 1)
{
// blah
}
else
{
$users = new Template('users');
$results = array();
while($result = $results_d->fetch_assoc())
{
foreach($result as $key => $value)
{
$results[$key] = $value;
}
}
$users->replace_while('results', $results);
}
|
|
|
|