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 */