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

Can't get loop working in plugin...

Development and Programming

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

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;
               }
           }
          
    }
    
  }
       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

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}
       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Hmmm. Are those Memorial Day crickets I hear chirping?

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

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;
}
       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Sorry for the lag. This worked out perfectly, thanks so much! 😊

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

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?

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

How about:

// ...
if (isset($row[$val]))
{
    if ($val == 'a_timestamp')
    {
        $row[$val] = $LOC->set_human_time($row[$val]);
    }
    $tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
}
       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Woot! I’m going to owe you money soon aren’t I? As soon as i get my passport I’ll need to drive down Portland way and buy you guys and gals a few rounds.

       

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.