Search code examples
codeigniterpyrocms

Issue calling variables into query with pyro plugin


I have a plugin that gets different items depending on number in the table, portfolio type, etc.

So far the type is working but I can't get the limit offset to work, this is code igniter.

        $type = $this->attribute('type', '');
        $portfolioNum = $this->attribute('portfolioNum', '');

        if ($portfolioNum !=0){
            $portfolioNum = settype($portfolioNum, "integer");
        }   

            $data = $this->db->select('portfolio.*, artists.artist_name event_title, artists.artist_slug event_slug')
                    ->from('portfolio')
                    ->where('portfolio_type', $type) 
                    ->where('portfolio_selected', 'Selected')
                    ->limit(1, 0)
                    ->join('artists_portfolio', 'portfolio.id = artists_portfolio.portfolio_id', 'LEFT')
                    ->join('artists', 'artists.id = artists_portfolio.row_id', 'LEFT')
                    ->get()
                    ->result();

This works fine, getting the first item in the table with portfolio_type.

When I try to add a variable in instead of the offset 0 it breaks.

        $type = $this->attribute('type', '');
        $portfolioNum = $this->attribute('portfolioNum', '');

        if ($portfolioNum !=0){
            $portfolioNum = 1;
        }   

            $data = $this->db->select('portfolio.*, artists.artist_name event_title, artists.artist_slug event_slug')
                    ->from('portfolio')
                    ->where('portfolio_type', $type) 
                    ->where('portfolio_selected', 'Selected')
                    ->limit(1, $portfolioNum)
                    ->join('artists_portfolio', 'portfolio.id = artists_portfolio.portfolio_id', 'LEFT')
                    ->join('artists', 'artists.id = artists_portfolio.row_id', 'LEFT')
                    ->get()
                    ->result();

And called in the template

{{TheSitePlugin:getSelected type="Production"  portfolioNum="1"}}{{/TheSitePlugin:getSelected}}

Solution

  • I'm pretty sure the problem is in those lines:

    if ($portfolioNum !=0){
      $portfolioNum = 1;
    } 
    

    From what I'm seeing, $portfolioNum is never 0, and therefor will always be set to 0.

    Change

    $portfolioNum = $this->attribute('portfolioNum', '');
    

    to

    $portfolioNum = $this->attribute('portfolioNum', 0);
    

    and delete the whole if clause. This way, the Plugin class' attribute function will set $portfolioNum to 0, if no portfolioNum is set as tag attribute.

    But you still might want to check, if the portfolioNum actually is an integer and whatnot. And maybe als rename portfolioNum to "offset", since that is what it is.