Page 1 of 1

select only one category among categories

Posted: Tue Mar 27, 2012 9:40 pm
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

Re: select only one category among categories

Posted: Tue Mar 27, 2012 10:06 pm
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 !==.

Re: select only one category among categories

Posted: Wed Mar 28, 2012 4:59 pm
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