I'm trying to automate some listings and in doing so need to run some PHP, the PHP is used to detect which category is the same as as the current page's url title then outputs the ID of the category with the same url title.
I then want to apply this ID to a list of products so that it outputs the relevent items (like categories but giving the flexibility of using Structure).
However I'm guessing because i'm defining the variable on the same page it isn't working for some reason, any ideas? My PHP parsing in template is set to "Output".
{exp:channel:entries}
{categories}{if category_url_title == "{last_segment}"}<?php $category = '{category_id}'; ?>{/if}{/categories}
{/exp:channel:entries}
{exp:channel:entries category="<?php echo $category; ?>" dynamic="no" channel="products"}
<h2>{title}</h2>
{/exp:channel:entries}
Don't bother! Keep it simple:
Using {last_segment_category_id}
replace all your code above for:
{exp:channel:entries category="{last_segment_category_id}" dynamic="no" channel="products"}
<h2>{title}</h2>
{/exp:channel:entries}
The reason your code won't ever work is because you want the first part to run the EE first, then the PHP to save the variable, then you want the PHP to continue and set the second part, then you want EE to kick in again and use the PHP variable. It's one followed by the other, not jumping between the 2 I'm afraid.
Another way around this is utilising embeds:
1st Template:
{exp:channel:entries}
{categories}{if category_url_title == "{last_segment}"}{embed="my-templates/2nd-template" category="{category_id}"}{/if}{/categories}
{/exp:channel:entries}
2nd Template:
{exp:channel:entries category="{embed:category}" dynamic="no" channel="products"}
<h2>{title}</h2>
{/exp:channel:entries}