We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Prep Conditionals Q

Development and Programming

DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

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?

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Sorry, I should note above that, yes, i’m aware it’s commented out. That’s just where I left things at…

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

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.

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Thanks again Pascal (and yep, I did catch the missing $FNS call…)

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.