select only one category among categories

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
lazut
Forum Members
Forum Members
Posts: 15
Joined: Thu Dec 22, 2011 3:54 pm

select only one category among categories

Post by lazut »

hi

In product module when I use {get_template_vars} in detail template I get categorytext= Travertine, Paver,Tile because of one product has more than one category.
what I am trying to do is something like this
{if categorytext=='Travertine'}say yes {else }no {/if}
but only I get yes when I use it like this
{if categorytext=='Travertine, Paver,Tile'} so how can I get yes only for category "Travertine".
when I use $categorytext[0] I got only "T" which is capital of Taravertine .

how can I select only "Travertine" category among three categories..

thanks all
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: select only one category among categories

Post by Wishbone »

You can use the PHP function strpos.

http://php.net/manual/en/function.strpos.php

{if $categorytext|strpos:'Travertine'!==FALSE}yes{else}no{/if}

strpos returns the offset of the search string in the target, or FALSE if not found.. You can't use !=FALSE, because 0 (which is a valid offset) is identical to FALSE unless you use !==.
lazut
Forum Members
Forum Members
Posts: 15
Joined: Thu Dec 22, 2011 3:54 pm

Re: select only one category among categories

Post by lazut »

Thanks Wishbone it works. I wrote this

Code: Select all

{if $categorytext|strpos:'Pavers'!==FALSE}
{Products action="default" category='Pool Copings' sortby="random " pagelimit="4" }
{elseif $categorytext|strpos:'Pool Copings'!==FALSE}
{Products action="default" category='Pavers' sortby="random " pagelimit="4"}
{elseif $categorytext|strpos:'Tile'!==FALSE}
{Products action="default" category='Trim & Mouldings,Glass Tiles'  sortby="random " pagelimit="4"}
{elseif $categorytext|strpos:'Glass Tiles'!==FALSE}
{Products action="default" category='Mosaics,Trim & Mouldings'  sortby="random " pagelimit="4"}
{elseif $categorytext|strpos:'Mosaics'!==FALSE}
{Products action="default" category='Glass Tiles,Trim & Mouldings'  sortby="random " pagelimit="4"}
{elseif $categorytext|strpos:'Trim & Mouldings'!==FALSE}
{Products action="default" category='Glass Tiles,Mosaics'  sortby="random " pagelimit="4"}
{/if} 


I am trying to set related products matching by categories ..it seems this code is working now but I dont know whether It is right and efficient way or maybe there is better way to do that

appreciate your help
thanks
Post Reply

Return to “Modules/Add-Ons”