Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Conditional Test for Text String

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 07-30-2007, 05:58 PM
 
sharingsunshine sharingsunshine is offline
 

Senior Member
  
Join Date: Aug 2005
Location: Palmyra, VA
Posts: 149
 

Default 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
__________________
***********************
Visit Our Other Web Sites!
∙ Nutritional Herbs and Supplements
<a href="http://www.theherbsplace.com"> The Herbs Place </a>
∙ Frugal Ideas and a Forum To Submit Your Questions
<a href="http://www.thefrugallife.com"> The Frugal Life </a>
∙ Healthy and Natural Pet Products
<a href="http://www.healthypetcorner.com"> Healthy Pet Corner </a>
***********************

Joined 08/02/05
X-Cart Gold Version 4.3.2
Linux RedHat Enterprise
Reply With Quote
  #2  
Old 07-31-2007, 06:42 AM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default Re: Conditional Test for Text String

In PHP:

Code:
if (substr_count($mystring, "Buy 3 Save")) { print ("true"); } else { print ("false"); }
__________________
Richard Williams
Rogue Wave Limited

Initial Inventory Imports
Daily Inventory Updates
Daily Inventory Reports
Drop Ship Support
Order Export to Shipper/Supplier
Shopping Feeds That Work
Programming for X-Cart

richard@roguewavelimited.com
http://www.roguewavelimited.com
Reply With Quote
  #3  
Old 07-31-2007, 07:28 AM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default Re: Conditional Test for Text String

In a Smarty template:
Code:
{if "Buy 3 Save" eq $mystring|truncate:10} <bold>Buy three and save big!</bold> {else} No deal here. {/if}
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote
  #4  
Old 08-01-2007, 11:44 AM
 
sharingsunshine sharingsunshine is offline
 

Senior Member
  
Join Date: Aug 2005
Location: Palmyra, VA
Posts: 149
 

Default 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
__________________
***********************
Visit Our Other Web Sites!
∙ Nutritional Herbs and Supplements
<a href="http://www.theherbsplace.com"> The Herbs Place </a>
∙ Frugal Ideas and a Forum To Submit Your Questions
<a href="http://www.thefrugallife.com"> The Frugal Life </a>
∙ Healthy and Natural Pet Products
<a href="http://www.healthypetcorner.com"> Healthy Pet Corner </a>
***********************

Joined 08/02/05
X-Cart Gold Version 4.3.2
Linux RedHat Enterprise
Reply With Quote
  #5  
Old 08-01-2007, 01:27 PM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default Re: Conditional Test for Text String

That's almost it. Try this:

Code:
{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}
__________________
Richard Williams
Rogue Wave Limited

Initial Inventory Imports
Daily Inventory Updates
Daily Inventory Reports
Drop Ship Support
Order Export to Shipper/Supplier
Shopping Feeds That Work
Programming for X-Cart

richard@roguewavelimited.com
http://www.roguewavelimited.com
Reply With Quote
  #6  
Old 08-02-2007, 05:24 AM
 
sharingsunshine sharingsunshine is offline
 

Senior Member
  
Join Date: Aug 2005
Location: Palmyra, VA
Posts: 149
 

Default 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
__________________
***********************
Visit Our Other Web Sites!
∙ Nutritional Herbs and Supplements
<a href="http://www.theherbsplace.com"> The Herbs Place </a>
∙ Frugal Ideas and a Forum To Submit Your Questions
<a href="http://www.thefrugallife.com"> The Frugal Life </a>
∙ Healthy and Natural Pet Products
<a href="http://www.healthypetcorner.com"> Healthy Pet Corner </a>
***********************

Joined 08/02/05
X-Cart Gold Version 4.3.2
Linux RedHat Enterprise
Reply With Quote
  #7  
Old 08-02-2007, 04:20 PM
 
inebriate inebriate is offline
 

eXpert
  
Join Date: May 2006
Posts: 301
 

Default 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
this replaces the ellipses with a null string and truncates at exactly 5 characters instead of at the end of a word

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"
__________________
x-cart pro 4.0.18
linux
www.fabric8d.com (currently undergoing construction)
Reply With Quote
  #8  
Old 08-03-2007, 08:29 AM
 
sharingsunshine sharingsunshine is offline
 

Senior Member
  
Join Date: Aug 2005
Location: Palmyra, VA
Posts: 149
 

Default 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
__________________
***********************
Visit Our Other Web Sites!
∙ Nutritional Herbs and Supplements
<a href="http://www.theherbsplace.com"> The Herbs Place </a>
∙ Frugal Ideas and a Forum To Submit Your Questions
<a href="http://www.thefrugallife.com"> The Frugal Life </a>
∙ Healthy and Natural Pet Products
<a href="http://www.healthypetcorner.com"> Healthy Pet Corner </a>
***********************

Joined 08/02/05
X-Cart Gold Version 4.3.2
Linux RedHat Enterprise
Reply With Quote
  #9  
Old 08-03-2007, 10:51 AM
 
inebriate inebriate is offline
 

eXpert
  
Join Date: May 2006
Posts: 301
 

Default Re: Conditional Test for Text String

before your code
put

{$sales[sale].name}

and see what gets outputted
__________________
x-cart pro 4.0.18
linux
www.fabric8d.com (currently undergoing construction)
Reply With Quote
  #10  
Old 08-03-2007, 01:06 PM
 
sharingsunshine sharingsunshine is offline
 

Senior Member
  
Join Date: Aug 2005
Location: Palmyra, VA
Posts: 149
 

Default 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
__________________
***********************
Visit Our Other Web Sites!
∙ Nutritional Herbs and Supplements
<a href="http://www.theherbsplace.com"> The Herbs Place </a>
∙ Frugal Ideas and a Forum To Submit Your Questions
<a href="http://www.thefrugallife.com"> The Frugal Life </a>
∙ Healthy and Natural Pet Products
<a href="http://www.healthypetcorner.com"> Healthy Pet Corner </a>
***********************

Joined 08/02/05
X-Cart Gold Version 4.3.2
Linux RedHat Enterprise
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 05:14 AM.

   

 
X-Cart forums © 2001-2020