Hi, I’m thinking I’m missing something extremely simple here. I’m trying to output a looping list of results from a database query; however, only the first row’s results are displayed when I load the page. Here’s the function:
function showitems()
{
global $DB,$TMPL;
if ($TMPL->fetch_param('entry_id')) {
$entry_id = $TMPL->fetch_param('entry_id');
$query = $DB->query("SELECT tob.*, tot.* FROM t_objects tob, t_objects_type tot WHERE tob.object_id = tot.object_id AND tob.object_entry_id = $entry_id ORDER BY tob.object_added");
if ($query->num_rows == 0)
{
$this->return_data = "Sorry, there are no items available here at the moment.";
} else {
foreach($query->result as $row)
{
$tagdata = $TMPL->tagdata;
foreach ($TMPL->var_single as $key => $val)
{
if (isset($row[$val]))
{
$tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}
}
//$this->return_data .= $tagdata;
return $tagdata;
}
}
}
}
Just as a point of clarification, at this time I’m using a plugin I wrote to simply barf out a list, much like the example in the docs. Works fine but hardly flexible. So what I want is to be able to output looping values a la weblog:entries or query:
{class:function}
{title} {date}{foo...}
{/end}
You’re almost there. You’re returning a bit too early, the loop never gets to do any looping.
Give this a shot:
function showitems()
{
global $DB,$TMPL;
$output = '';
if ( ! $entry_id = $TMPL->fetch_param('entry_id'))
{
$TMPL->log_item("Missing parameter");
return '';
}
$query = $DB->query("SELECT tob.*, tot.* FROM t_objects tob, t_objects_type tot WHERE tob.object_id = tot.object_id AND tob.object_entry_id = $entry_id ORDER BY tob.object_added");
if ($query->num_rows == 0)
{
return "Sorry, there are no items available here at the moment.";
}
foreach($query->result as $row)
{
$tagdata = $TMPL->tagdata;
foreach ($TMPL->var_single as $key => $val)
{
if (isset($row[$val]))
{
$tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}
}
$output .= $tagdata;
}
return $output;
}
Hmm, another question. Now that this loop is functioning properly, I’m wondering where I can properly format a timestamp field that’s being returned. Normally I would do this:
$LOC->set_human_time($row['a_timestamp']);
But I don’t see an appropriate place in the loop to do it. Ideas?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.