First, many thanks to Pascal for some earlier questions. Having just delved into plugin and extension development against a self-imposed deadline, I’m learning much in a short time. Thanks for the assistance.
My current question is around how, or more accurately where, to prep conditionals. I have a db query in a plugin, that pulls out a variety of member data. I want to determine if there’s a user avatar or not in the template and choose to display an alternative if not. The code:
function here()
{
global $DB,$TMPL;
$output = '';
if ( ! $entry_id = $TMPL->fetch_param('entry_id'))
{
$TMPL->log_item("Missing parameter");
return '';
}
if ( ! $limit = $TMPL->fetch_param('limit'))
{
$limit = 5;
}
$query = $DB->query("SELECT DISTINCT tc.n_date,tc.entry_id,m.username,m.screen_name,m.avatar_filename
FROM tctable AS tc
INNER JOIN exp_members AS m ON tc.member_id = m.member_id
WHERE tc.entry_id = $entry_id
ORDER BY tc.n_date DESC LIMIT $limit");
if ($query->num_rows == 0)
{
return "foo";
}
foreach($query->result as $row)
// $cond = $row;
// $cond['avatar_filename'] = ( isset($row['allow_comments']) ) ? 'FALSE' : 'TRUE';
// $tagdata = $FNS->prep_conditionals($tagdata, $cond);
{
$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;
}
This errors out. My question is, where do I do the prep calls in here?
It’s me again 😉 .
Well, first off, it needs to be inside the curly brackets of the foreach. Right now you have a syntax error in there.
And then it’s essentially done before replacing the variables. You need a $tagdata variable before passing it as a parameter, so right after that assignment is a good spot to do it in this case.
foreach($query->result as $count => $row)
{
$tagdata = $TMPL->tagdata;
// If we want extra vars - this is what it might look like
$row['count'] = $count + 1;
$tagdata = $FNS->prep_conditionals($tagdata, $row);
foreach ($TMPL->var_single as $key => $val)
{
if (isset($row[$val]))
{
$tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}
}
$output .= $tagdata;
}
You also need $FNS in your global declaration to be able to access it.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.