|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Hi,
So what are you using to print the variable in the template? e.g. {$variable} Or {php}echo $variable{/php} Or what? And do you use any modifier for the variable, or do you just use the variable as assigned to? Guido -----Oorspronkelijk bericht----- Van: George Pitcher [mailto:george.pitcher@ingenta.com] Verzonden: vrijdag 17 november 2006 16:45 Aan: Smarty-General Onderwerp: RE: [SMARTY] problem displaying float > On Fri, Nov 17, 2006 at 02:10:12PM -0000, George Pitcher wrote: > [...] > > The actual code for this page is long and complex, but the > relevant snippet > > looks like: > > ================================================== ========= > > $fee = [web-service response (in dollars and cents)] > > settype($fee, "float"); > > $fee_type = gettype($fee); > > $range = explode(",",strim($pr)); > > $rangecounter = count($range); > > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > > $ccc_supp = floatval($rangecounter * 3); > > settype($ccc_supp, "float"); > > $fee2 = $fee + $ccc_supp; > > > > $smarty->assign('ccc_fee',($rt!='Granted'?'Not available through CCC: > > '.($rt!=''?$rt:'no reason given'):'The CCC QuickPrice for this > request is: > > $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms")); > > ================================================== ========= > > $pr is a pagerange and in the example is 9-27. extended range might be > > 1-3,9-27, which would then add $3 to the $fee variable. > > ================================================== ========= > > > > If I echo $fee, for my example, I get 160.44. If I echo $fee2, > I get 160.00. > > But all I've done is add '0' to the $fee variable. > > this is your third post on the topic and you still refused to show us a > single line of template code. > > now you show us a case where you trigger the error without any smarty > interference (I think we can ignore the $smarty->assign() and your > error occurs with or without it). > > so what has this to do with smarty anyway? > messju I do all my calculations in PHP and let Smarty handle the display - I thought that's what templates were all about. The problem I have is that my display via $smarty-assign() was giving me an incorrect display. I then, as usual with such an error, thought there was something wrong with the PHP code, so started echoing the output at the various steps. I still thought the problem was in PHP, but when I decided to put the process into a php file that did not include and $smarty interaction, the display was fine, which is why I'm asking the question on this list. If no-one has a solution, I'll need to think of a different way to display the price to my users. Cheers George -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Hi George,
maybe you did not assign $fee properly/at all... There should be a line of code in your php-script like: $smarty->assign('fee',$fee); Within your template you then state {$fee} or as messju recommended {"%.2f"|sprintf:$fee} The latter modifies a string to get the proper number of floating values (or however these are called).... Hope that s... Peggy |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Guido and Peggy,
> So what are you using to print the variable in the template? > > e.g. > {$variable} > > Or {php}echo $variable{/php} $fee = [web-service response (in dollars and cents)] settype($fee, "float"); $fee_type = gettype($fee); $range = explode(",",strim($pr)); $rangecounter = count($range); $rangecounter = ($rangecounter>1?$rangecounter-1:0); $ccc_supp = floatval($rangecounter * 3); settype($ccc_supp, "float"); $fee2 = $fee + $ccc_supp; $smarty->assign('ccc_fee','The CCC QuickPrice for this request is: $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms"); > > Or what? > > And do you use any modifier for the variable, or do you just use the > variable as assigned to? > maybe you did not assign $fee properly/at all... > > There should be a line of code in your php-script like: > $smarty->assign('fee',$fee); > Within your template you then state {$fee} or as messju recommended > {"%.2f"|sprintf:$fee} > The latter modifies a string to get the proper number of floating values > (or however these are called).... All is handled in PHP before assignation. I'm only asking $smarty to display my variable. I've been using Smarty for 3 years now - across over 20 websites and this is the first real hassle I've encountered. Anyway, I'm off for the weekend now. Cheers George |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
Hi George,
My test script using your example does not show anything wrong: My PHP: > <?php > > include_once "Smarty/Smarty.class.php"; > > $fee = 1.22; > settype($fee, "float"); > $fee_type = gettype($fee); > $range = explode(",","1-3"); > $rangecounter = count($range); > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > $ccc_supp = floatval($rangecounter * 3); > settype($ccc_supp, "float"); > $fee2 = $fee + $ccc_supp; > > echo 'The CCC QuickPrice for this request is: $' . > sprintf("%01.2f",$fee2)."<br>$ccc_terms"; > > $rt = "Granted"; > > // create object > $smarty = new Smarty; > $smarty->assign('fcc',($rt!='Granted'?'Not available through CCC:' . > ($rt!=''?$rt:'noreason given'):'The CCC QuickPrice for this request is: $' > . sprintf("%01.2f",$fee2)."<br>$ccc_terms")); > > $smarty->template_dir = "D:\\web\\WAMP\\www"; > $smarty->compile_dir = "D:\\web\\WAMP\\www\\templates_c"; > // display it > $smarty->display('test.tpl'); > > ?> My Template "test.tpl" > {$fcc} The result when I use this code through the browser: > The CCC QuickPrice for this request is: $1.22 > The CCC QuickPrice for this request is: $1.22 Also for smarty you will always need a template. Thus your code should contain a $smarty->display(filename) which tell you which template you are using. Smarty is not a pretty printing tool, but a template engine. So we need variables (Assigned by $smarty->assign) and a template (Gives by $smarty->display) to create a result. Cheers, Guido -----Oorspronkelijk bericht----- Van: George Pitcher [mailto:george.pitcher@ingenta.com] Verzonden: vrijdag 17 november 2006 16:45 Aan: Smarty-General Onderwerp: RE: [SMARTY] problem displaying float > On Fri, Nov 17, 2006 at 02:10:12PM -0000, George Pitcher wrote: > [...] > > The actual code for this page is long and complex, but the > relevant snippet > > looks like: > > ================================================== ========= > > $fee = [web-service response (in dollars and cents)] > > settype($fee, "float"); > > $fee_type = gettype($fee); > > $range = explode(",",strim($pr)); > > $rangecounter = count($range); > > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > > $ccc_supp = floatval($rangecounter * 3); > > settype($ccc_supp, "float"); > > $fee2 = $fee + $ccc_supp; > > > > $smarty->assign('ccc_fee',($rt!='Granted'?'Not available through CCC: > > '.($rt!=''?$rt:'no reason given'):'The CCC QuickPrice for this > request is: > > $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms")); > > ================================================== ========= > > $pr is a pagerange and in the example is 9-27. extended range might be > > 1-3,9-27, which would then add $3 to the $fee variable. > > ================================================== ========= > > > > If I echo $fee, for my example, I get 160.44. If I echo $fee2, > I get 160.00. > > But all I've done is add '0' to the $fee variable. > > this is your third post on the topic and you still refused to show us a > single line of template code. > > now you show us a case where you trigger the error without any smarty > interference (I think we can ignore the $smarty->assign() and your > error occurs with or without it). > > so what has this to do with smarty anyway? > messju I do all my calculations in PHP and let Smarty handle the display - I thought that's what templates were all about. The problem I have is that my display via $smarty-assign() was giving me an incorrect display. I then, as usual with such an error, thought there was something wrong with the PHP code, so started echoing the output at the various steps. I still thought the problem was in PHP, but when I decided to put the process into a php file that did not include and $smarty interaction, the display was fine, which is why I'm asking the question on this list. If no-one has a solution, I'll need to think of a different way to display the price to my users. Cheers George -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
smarty displays what you assign. nothing more nothing less.
again: the fact that you use smarty does not make this a smarty issue. maybe you have some locale settings that disturb your float-conversions? like this braindead behaviour of php: setlocale(LC_ALL, "de_DE.UTF-8"); $foo = 10001/100; echo $foo, "\n"; echo floatval("$foo"), "\n"; gives on my system: 100,01 100 On Fri, Nov 17, 2006 at 04:12:19PM -0000, George Pitcher wrote: > Guido and Peggy, > > > So what are you using to print the variable in the template? > > > > e.g. > > {$variable} > > > > Or {php}echo $variable{/php} > > > $fee = [web-service response (in dollars and cents)] > settype($fee, "float"); > $fee_type = gettype($fee); > $range = explode(",",strim($pr)); > $rangecounter = count($range); > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > $ccc_supp = floatval($rangecounter * 3); > settype($ccc_supp, "float"); > $fee2 = $fee + $ccc_supp; > > $smarty->assign('ccc_fee','The CCC QuickPrice for this request is: > $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms"); > > > > > Or what? > > > > And do you use any modifier for the variable, or do you just use the > > variable as assigned to? > > > maybe you did not assign $fee properly/at all... > > > > There should be a line of code in your php-script like: > > > $smarty->assign('fee',$fee); > > > Within your template you then state {$fee} or as messju recommended > > {"%.2f"|sprintf:$fee} > > The latter modifies a string to get the proper number of floating values > > (or however these are called).... > > All is handled in PHP before assignation. I'm only asking $smarty to display > my variable. > > I've been using Smarty for 3 years now - across over 20 websites and this is > the first real hassle I've encountered. > > Anyway, I'm off for the weekend now. > > Cheers > > George |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
George Pitcher wrote:
> I do all my calculations in PHP and let Smarty handle the display - I > thought that's what templates were all about. Exactly. Assign the value to the template object as a string, not as a float, and see what you get. Shouldn't matter, but at least then you'll get the value in a more-or-less literal form. There generally oughtn't be any need to using sprintf() in the template code. Also, do you have error_reporting set to at least E_ALL? -- Max Schwanekamp NeptuneWebworks.com 541-517-9064 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
George Pitcher wrote:
> Guido and Peggy, > >> So what are you using to print the variable in the template? > $fee = [web-service response (in dollars and cents)] > settype($fee, "float"); > $fee_type = gettype($fee); > $range = explode(",",strim($pr)); > $rangecounter = count($range); > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > $ccc_supp = floatval($rangecounter * 3); > settype($ccc_supp, "float"); > $fee2 = $fee + $ccc_supp; > > $smarty->assign('ccc_fee','The CCC QuickPrice for this request is: > $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms"); > None of the above displays anything; you assign a value to the Smarty, but don't show the call to $smarty->display() or (and this is what both I and others have asked for) what you put in the template. For the sake of argument I'll assume your template just says {$ccc_fee} I think it's clear that the value being assigned is wrong, so this isn't a Smarty issue, but a PHP one. This list gets more than its fair share of off-topic PHP queries hence some reluctance to assist in non-Smarty problems. However, I'll make some suggestions then bow out of the topic (I'll assist off-list if you're really stuck). Why are you using lots of settype() calls? Is that the result of some hacks to try and resolve this problem? It's very rare that settype should really be needed anywhere. What does the following code give you? $fee = [web-service response (in dollars and cents)] var_dump($fee); $range = explode(",",strim($pr)); $rangecounter = count($range); $rangecounter = ($rangecounter>1?$rangecounter-1:0); $ccc_supp = $rangecounter * 3; var_dump($range, $rangecounter, $ccc_supp); $fee2 = $fee + $ccc_supp; var_dump($fee2, sprintf("%01.2f",$fee2)); Note that I use var_dump as that tells you both the value and the type in one. > All is handled in PHP before assignation. I'm only asking $smarty to display > my variable. > The implication, which there's been nothing to suggest to the contrary here, is therefore that the variable is wrong before it gets to Smarty. Unless, that is, you're doing something in Smarty other than just displaying what its sent, such as formatting code. That's why seeing the relevant part of the template would . > I've been using Smarty for 3 years now - across over 20 websites and this is > the first real hassle I've encountered. > I'm 90% sure this is nothing to do with Smarty, but I'm quite open to being proved wrong on that. -- Mark Rogers More Solutions Ltd :: 0845 45 89 555 |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
George Pitcher wrote:
> The actual code for this page is long and complex, but the relevant snippet > looks like: > ================================================== ========= > $fee = [web-service response (in dollars and cents)] > settype($fee, "float"); > $fee_type = gettype($fee); > $range = explode(",",strim($pr)); > $rangecounter = count($range); > $rangecounter = ($rangecounter>1?$rangecounter-1:0); > $ccc_supp = floatval($rangecounter * 3); > settype($ccc_supp, "float"); > $fee2 = $fee + $ccc_supp; > > $smarty->assign('ccc_fee',($rt!='Granted'?'Not available through CCC: > '.($rt!=''?$rt:'no reason given'):'The CCC QuickPrice for this request is: > $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms")); > ================================================== ========= > I just tried the following code, which I suggest you also try: =========================================== <?php $fee = '160.44'; $pr = '9-27'; $ccc_terms = ''; $rt = 'Granted'; settype($fee, "float"); $fee_type = gettype($fee); $range = explode(",",trim($pr)); $rangecounter = count($range); $rangecounter = ($rangecounter>1?$rangecounter-1:0); $ccc_supp = floatval($rangecounter * 3); settype($ccc_supp, "float"); $fee2 = $fee + $ccc_supp; $ccc_fee = $rt!='Granted'?'Not available through CCC: ' .($rt!=''?$rt:'no reason given'):'The CCC QuickPrice for this request is: $' .sprintf("%01.2f",$fee2)."<br>$ccc_terms"; var_dump($fee, $range, $rangecounter, $ccc_supp, $fee2, $ccc_fee); ?> =========================================== It is as close I could get to your exact code, notice I substitute "trim" for "strim", and calculated and 'var_dump'ed $ccc_fee rather than using Smarty. The results I get are: =========================================== float(160.44) array(1) { [0]=> string(4) "9-27" } int(0) float(0) float(160.44) string(51) "The CCC QuickPrice for this request is: $160.44<br>" =========================================== All of those look fine to me. If you get the same results, and yet $smarty->assign('ccc_fee',$ccc_fee); ... in your code and {$ccc_fee} ... in your template shows something different, then I would be very surprised. However, suppose $fee is actually '160. 44' (note the space after the ..), or as Messju suggested '160,44' (comma instead of .) then the conversion to float will give 160 not 160.44 and the results you are getting. So corruption of the value of $fee would be where I'd go looking. You should also be careful that a price of '1,234.44' doesn't get converted to $1 by your method, for the same reasons. -- Mark Rogers More Solutions Ltd :: 0845 45 89 555 |
|
![]() |
| Outils de la discussion | |
|
|