0

I am working in a channel entries tag. I want to return an array of category names that are selected in the entry, if they are in group_id 4. This is what I have so far... however, it is returning all my categories in group_id 4 not just the ones selected in that entry.

$this->EE =& get_instance();
$res = $this->EE->db->select('cat_name')->where('group_id', 4)->get('categories')->result_array();
    foreach ($res as $r)
    {
        $entryregionArr[] = $r['cat_name'];
    }
    if (in_array($_GET['region'], $entryregionArr)) {
        echo "Africa is in this array";
    }

   print_r($res);

I am then going to see if my $_Get value is in the array. Right now with all the categories in group_id 4 returning, Africa is always in the array. so it returns everytime.

1

You need to have JOIN statement for it. Like:

$this->EE =& get_instance();
$entry_id = '[FETCH ENTRY ID]';
$res = $this->EE->db->select('c.cat_name')
                ->from('categories c')
                ->join('category_posts cp', 'cp.cat_id = c.cat_id')
                ->join('channel_titles ct', 'c.entry_id = cp.entry_id')
                ->where('c.group_id', 4)
                ->where('ct.entry_id', $entry_id)
                ->result_array();

I hope, it would help you.

| improve this answer | |
  • I can't seem to figure out why the page is loading as straight up code when I place this code into it. I should place it between my php open and close tags, correct? Or do I need to do anything with the FETCH ENTRY ID? – JayB Jul 26 '13 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.