0

I am using in the below code, and it is not returning anything.

Details:

  • php is allowed on my template
  • parsing is set on output.
  • the '[FETCH ENTRY ID]' does not return my entry id (I can put in the ee tag {entry_id}, but than it only returns one entry_id. I think I need it to stay in php without any ee tags at this point.

All ideas any welcome!

{exp:channel:entries channel="property" category_group="4"}
<?php
    $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();
?>
{exp:channel:entries}
  • Use the Tools -> data -> SQL Manager tool in EE admin and then use the Database Query Form to run the SQL and test your syntax. – Peter Lewis Jul 29 '13 at 16:16
  • Is this "Query Form" only for the php part of the code, and not the opening and closing channel tags? Should it return the results, or just give an error if there is an error with the syntax? – JayB Jul 29 '13 at 20:08
  • No it's to test the syntax of your SQL - so the "SELECT c.cat_name FROM exp_categories c JOIN exp_category_posts cp ON cp.cat_id = c.cat_id JOIN exp_channel_titles ct ON cp.entry_id = ct.entry_id WHERE c.group_id = 4 AND ct.entry_id = ##" - that way you can ensure it returns something and is correct, then move on to something else that may be causing the problem. – Peter Lewis Jul 30 '13 at 8:21
1

Shouldn't the second join use ct instead of cp?

| improve this answer | |
  • I just tried it that way, no luck. – JayB Jul 29 '13 at 16:24
  • Have you tried using the Query module instead of PHP? ellislab.com/expressionengine/user-guide/modules/query – Ponder The Web Jul 29 '13 at 20:15
  • I/m afraid I do not have the knowledge for that, this is as far as I can get and it only results in errors. {exp:query sql=" SELECT c.cat_name from exp_categories c INNER JOIN exp_category_posts cp, cp.cat_id = c.cat_id ON exp_channel_titles ct c.entry_id = cp.entry_id, WHERE c.group_id = 4 AND ct.entry_id = {entry_id}; "} {cat_name} {/exp:query} – JayB Jul 29 '13 at 23:09
  • The second join should be cp.entry_id = ct.entry_id. I answered again so the formatting would be in place and be easier to read. – Ponder The Web Jul 30 '13 at 3:27
  • I'll try again. – JayB Jul 30 '13 at 13:18
0

It should be something along the lines of this:

{exp:channel:entries channel="property" category_group="4"}

    {exp:query sql="SELECT c.cat_name FROM exp_categories c 
        JOIN exp_category_posts cp ON cp.cat_id = c.cat_id 
        JOIN exp_channel_titles ct ON cp.entry_id = ct.entry_id 
        WHERE c.group_id = 4 AND ct.entry_id = {entry_id}"} 
        {cat_name}
    {/exp:query}

{/exp:channel:entries}

I think part your original problem may be that your closing {exp:channel:entries} didn't have the slash so it wasn't parsing the entry_id like it should.

| improve this answer | |
  • I'll give it a go. – JayB Jul 30 '13 at 13:17
0

Although I think @Ponder The Web has answered your question directly, why are you using SQL in the first place?

{exp:channel:entries channel="property" category_group="4"}
    {categories show_group="4"} 
        {category_name}
    {/categories}
{/exp:channel:entries}

Ref: Categories Documentation

Also regarding your original question:

the '[FETCH ENTRY ID]' does not return my entry id (I can put in the ee tag {entry_id}, but than it only returns one entry_id. I think I need it to stay in php without any ee tags at this point.

[FETCH ENTRY ID] will do nothing, I've no idea where this has come from - it looks like Wordpress syntax...?

{entry_id} should be set on each loop though with your PHP code being duplicated for each entry it finds, so {entry_id} should work fine if the other issues (e.g. closing channel:entries tag) are resolved.

Updated for new information: using Server Variables

{exp:channel:entries channel="property" category_group="4"}
    {categories show_group="4"}
        {if "{exp:server_var:get var="region"}" == {category_id}}
            {category_name}
        {/if}
    {/categories}
{/exp:channel:entries}

or

{exp:channel:entries channel="property" category_group="4"}
    {exp:server_var:get}
        {categories show_group="4" show="{region}"}
            {category_name}
        {/categories}
    {/exp:server_var:get}
{/exp:channel:entries}

Both assumes that $_GET["region"] is the category ID.

| improve this answer | |
  • I need to check if the $_GET['region'] chosen on the front end, is in the array of category names from the entries selected regions. than I can use it in a comparison within a if:else statement to filter entries. However, when I make an array of category_names is has an interesting indexing, and I can't get it to work the way you suggest. I have went through a lot with this challenge, so, so, tired.. thanks for clearing up the [fetch entry id] I have tried {entry_id}, however with no positive results. I think parsing is a big issue, trying to switch from ee tags to php and back again. – JayB Jul 30 '13 at 13:16
  • That wasn't mentioned :) Try: devot-ee.com/add-ons/server-variable. To be able to help, we need all the information - I've updated my answer for you, but I expect there's still more to what you are trying to do. – Peter Lewis Jul 30 '13 at 13:27
  • I will look at the {server_var:get}, I am using GET/POST Parameters like this {exp:parameters:get name='region'} to do my comparison currently, but it is not a tag pair like {server_var:get}. I'll let you know how it goes. – JayB Jul 30 '13 at 17:31

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.