![]() |
Conditional Test for Text String
Hi,
I need to know how to test a variable for the first 10 letters of the variable string. For example, I have a variable that could have the following values. 1. Buy Now Get ... 2. Buy 3 Save ... 3. $1 OFF ... I need to test the first 10 characters for the Buy 3 Save $25.80 or it might say Buy 3 Save $16.80. In either case I need the {if} test to test for the Buy 3 Save presence and ignore the others. How would I do a partial text compare like that? Thanks, Randal |
Re: Conditional Test for Text String
In PHP:
Code:
if (substr_count($mystring, "Buy 3 Save")) { |
Re: Conditional Test for Text String
In a Smarty template:
Code:
{if "Buy 3 Save" eq $mystring|truncate:10} |
Re: Conditional Test for Text String
Thanks for getting back to me. However, I am not getting it to work. Can you take a look at my work and see if something is wrong?
This is what is showing in the $sales array on the page I want the condition to work on name => Buy 3 - SAVE $11.25 - Glyco Essentials Here is my code on product.tpl {if $sales} {elseif $sales and $sales[sale].name|truncate:5 eq "Buy 3"} {include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {else} {include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {/if} My logic: If $sales is present then don't show the Buy Now button at all unless the name is Buy 3. If no $sales is present then show the Buy Now button after all. Any help will be greatly appreciated. Thanks, Randal |
Re: Conditional Test for Text String
That's almost it. Try this:
Code:
{if $sales and $sales[sale].name|truncate:5 eq "Buy 3"} |
Re: Conditional Test for Text String
Here is what I tried and it didn't work for some reason:
{if $sales and $sales[sale].name|truncate:5 eq "Buy 3"} {include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {elseif $sales} {else} {{include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {/if} Everything works but the first condition. Using this code the buy now button doesn't display on anything that starts with Buy 3. Seems to me the string compare isn't working. However, I have looked on the smarty forum and haven't found anything like it yet. Please help me get this answer. Thanks, Randal |
Re: Conditional Test for Text String
for the first condition remove the first $sales since its redundant...you check if an array exists and you call an array index
as for your main question, use Code:
$sales[sale].name|truncate:5:"":true if you use just truncate:5, what happens is that the string will get truncated to 5 characters INCLUDING the ellipses, ie: "Buy 3 blahblahblah" gets truncated to "Bu..." and fails the comparison to "Buy 3" |
Re: Conditional Test for Text String
Thanks for the clarification on the ellipses. I changed the code but it still doesn't work.
Here is what I have: {if $sales[sale].name|truncate:5:"":true eq "Buy 3"} {include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {elseif $sales} {else} {include file="buttons/add_to_cart.tpl" style="button" href="javascript: if(FormValidation()) document.orderform.submit();"} {/if} I reread the material on smarty.net about truncate and now I see the ... but they sure didn't make a big emphasis of it. Glad you saw that. Thanks, Randal |
Re: Conditional Test for Text String
before your code
put {$sales[sale].name} and see what gets outputted |
Re: Conditional Test for Text String
I am not real strong on Smarty so here is what I did:
I put that variable in product.tpl which is where I have the code that I am testing - and it didn't show up. When I run webmaster mode it is there in this form: {$sales} Array (1) 0 => Array (9) sales_id => 224 sku => 14077-5 name => Buy 3 - SAVE $11.25 - Glyco Essentials productid => 604 price => 63.60 start => 1185660000 amount => 3 expires => 1186264800 active => Y this is showing on the page that has the Buy 3 in the name. When I put the code on the specials.tpl page which has the section and loop to read all the specials it outputted it fine. It shows only what you see above. I need to be on the product.tpl page because that is where the add to cart button is being displayed. Please tell me how to get this string compared properly. I appreciate your help and hope you can tell me what is wrong. Thanks, Randal |
All times are GMT -8. The time now is 07:24 PM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.