XF 1.5 Xen:if "has" instead of "equal"

JordanH

Well-known member
I'm trying to display multiple categories of an addon.
In the database it shows like this:
https://snap.me/SW3Z8f
Some of them have 1 category, some of them have multiple.

How can I show if a "product" is in a category in a template?
If I were to do something like this:

HTML:
<xen:if is="{$product.category_ids} == 7">
    <a href="#">Indie</a>,
</xen:if>
<xen:if is="{$product.category_ids} == 5">
    <a href="#">Adventure</a>,
</xen:if>
<xen:if is="{$product.category_ids} == 4">
    <a href="#">Action</a>,
</xen:if>
etc, etc for every category with it's respective id and name. Only the first category in the database would show for the product, not multiple if the product had multiple categories.
 
PHP:
<xen:if is="in_array(7, {$product.category_ids})">
    <a href="#">Indie</a>,
</xen:if>
 
Ah oops, I mistakenly thought category_ids was an array but it's actually a string.

In your PHP, use something like:
PHP:
$product['selCategoryIds'] = explode(',', $product['category_ids']);

Pass that to your template and then you can use what I posted above (albeit with a small tweak):

PHP:
<xen:if is="in_array(7, {$product.selCategoryIds})">
    <a href="#">Indie</a>,
</xen:if>
 
Sorry for such a long wait to reply. I have been busy busy!

So, everything seems to have worked here. Can see in the sidebar:
https://www.gamerebels.com/product/playerunknowns-battlegrounds.97/

Thanks for the help!
However, I have 1 last final question regarding something like this.

in the code here:

PHP:
<xen:if is="in_array(7, {$product.selCategoryIds})">
    <a href="#">Indie</a>,
</xen:if>

Where it says Indie, if I wanted to display the number there as well, How would I do so?
When pulling data from other items such as the title you simply put {$product.title} in the code and it displays the title. However, for something that has multiple options with commas, how would I go about doing that?
 
Back
Top Bottom