|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
litronics Smarty n00b
Joined: 25 Nov 2015 Posts: 3
|
Posted: Wed Nov 25, 2015 11:09 am Post subject: Compare evaluates wrong within IF statement |
|
|
Hi all,
I am running a smiple IF statment as follows:
Code: |
{if a > b}
do something
{/}
|
If I assign a=99.99 and b=78.90 --> do something is executed
If I assign a=149.99 and b=78.90 --> do something is NOT executed
The question now is - am I understood something wrong or is there an issue doing a compare within my smarty implementation? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Nov 25, 2015 1:07 pm Post subject: |
|
|
Please provide a working code sample. |
|
Back to top |
|
litronics Smarty n00b
Joined: 25 Nov 2015 Posts: 3
|
Posted: Wed Nov 25, 2015 1:45 pm Post subject: |
|
|
@AnrDaemon
no problem - the code used in my shop system is as follows:
Code: |
{if $sArticle.pseudoprice > $sArticle.price}
<div>old price: {$sArticle.pseudoprice}</div>
{/if}
|
Both $sArticle variables are populated based on values within a MySql database from my shop system.
I get different results based on the following values:
Quote: | $sArticle.pseudoprice = 99,99
$sArticle.price = 78,90
old price is shown |
and if I set these values:
Quote: | $sArticle.pseudoprice = 149,99
$sArticle.price = 78,90
old price is NOT shown |
I also have verified the value of both variables by printing them directly on the screen - and they are populated exactly as shown above. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Nov 25, 2015 1:50 pm Post subject: |
|
|
1. WORKING code example. I can't run your template without having the PHP code invoking it. I could probably fake it but I'll never know, if my guess was matching your actual environment.
2. Are these variables actual numbers or formatted strings, when they hit the template? (The latter is quite common for prices to avoid '.999999999 cents' syndrome.) |
|
Back to top |
|
litronics Smarty n00b
Joined: 25 Nov 2015 Posts: 3
|
Posted: Wed Nov 25, 2015 1:59 pm Post subject: |
|
|
@AnrDaemon:
thanks for the hint!
I was not assuming that the shop system might store integers as strings.
Finally I changed my compare as follows:
Code: | {if (int) $sArticle.pseudoprice > (int) $sArticle.price} |
And it works now. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Nov 25, 2015 2:37 pm Post subject: |
|
|
Do note that it would only work if prices are different in integer range.
Code: | $ cat test.php; ./test.php
<?php
$x = (int) "2,2" > (int) "2,1";
var_dump($x);
bool(false) |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|