ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Check if a category is selected in a channel entry

March 03, 2012 5:08pm

Subscribe [2]
  • #1 / Mar 03, 2012 5:08pm

    Guido Neele

    8 posts

    This question may be related to a resolved thread.

    I posted some code in the previous thread but it was very inefficient. When you would have an entry with 100 categories, the previous plugin would make 100 SQL queries. That of course could be brought back to 1 query. So I updated my code and here it is. I’m also planning to post this on devot:ee.

    <?php
    
    if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    $plugin_info = array(
        'pi_name' => 'Is_checked',
        'pi_version' =>'1.0',
        'pi_author' =>'Guido Neele',
        'pi_author_url' => 'http://neele.name/',
        'pi_description' => 'Check if a category is checked in an entry'
    );
    
    class Is_checked {
            
        var $return_data = "";
        var $entry_id = "";
    
        function Is_checked()
        {
            $this->__construct();
        }
    
        function __construct() 
        {
            $this->EE =& get_instance();
            $this->entry_id = $this->EE->TMPL->fetch_param("entry_id");
            $cat_id = $this->EE->TMPL->fetch_param("category_id");
            
            $selected_categories = $this->_selected_categories();
    
            if (in_array($cat_id, $selected_categories)) {
                $this->return_data = 1;
            }
            else {
                $this->return_data = 0;
            }
        }
        
        
        
        /**
         * Get the file directory data and keep it stored in the cache
         * 
         * @return array Array of file directories
         */
        private function _selected_categories()
        {
            if ( ! $this->EE->session->cache(__CLASS__, 'selected_categories'))
            {
                $this->EE->db->where("entry_id", $this->entry_id);
                $query = $this->EE->db->get("exp_category_posts");
            
                if ($query->num_rows() > 0)
                {
                    $cache = array();
            
                    foreach ($query->result() as $row)
                    {
                        $cache[] = $row->cat_id;
                    }
            
                    $this->EE->session->set_cache(__CLASS__, 'selected_categories', $cache);
                }
            }
            
            return $this->EE->session->cache(__CLASS__, 'selected_categories');
        }
        
    }
    
    /* End of file pi.is_checked.php */ 
    /* Location: ./system/expressionengine/third_party/is_checked/pi.is_checked.php */

     

  • #2 / Mar 05, 2012 12:46pm

    Shane Eckert

    7174 posts

    Hello James Bomb,

    Thank you for posting this. I and the community thank you. I think it would be awesome if you posted that to Devot:ee as well. Nice work and thank you for considering the community when you are writing code and problem solving.

    Is there anything else I can help you with?

    Cheers!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases