|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I'm having a problem - only on Smarty pages. The following code: $fee = 160.44; $fee2 = $fee + 0; // I have a reason for adding zero; echo $fee."<br>"; echo $fee2."<br>"; settype($fee, "float"); echo gettype($fee); gives me 160.44 160.44 float on a non-Smarty page and 160.44 160 NULL when echo'd on a page using a Smarty template. I've tried using sprintf() with no success. Does anyone have any idea why this is happening? George in Oxford |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
George Pitcher wrote:
> The following code: > > $fee = 160.44; > $fee2 = $fee + 0; // I have a reason for adding zero; > echo $fee."<br>"; > echo $fee2."<br>"; > settype($fee, "float"); > echo gettype($fee); > I'd be more interested to know what gettype shows before the settype, and also for gettype($fee2). > [...] > and > > 160.44 > 160 > NULL > > when echo'd on a page using a Smarty template. > What does your Smarty template look like? Where are you doing the "maths" (the +0 bit) - within the template? I'm really not clear what you're assigning to the template, or how you're showing it. -- Mark Rogers More Solutions Ltd :: 0845 45 89 555 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Fri, Nov 17, 2006 at 11:52:23AM -0000, George Pitcher wrote:
[...] > I've tried using sprintf() with no success. {"%.2f"|sprintf:$fee} greetings messju > Does anyone have any idea why this is happening? > > George in Oxford |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> > The following code:
> > > > $fee = 160.44; > > $fee2 = $fee + 0; // I have a reason for adding zero; > > echo $fee."<br>"; > > echo $fee2."<br>"; > > settype($fee, "float"); > > echo gettype($fee); > > > > I'd be more interested to know what gettype shows before the settype, > and also for gettype($fee2). > > > [...] > > and > > > > 160.44 > > 160 > > NULL > > > > when echo'd on a page using a Smarty template. > > > > What does your Smarty template look like? Where are you doing the > "maths" (the +0 bit) - within the template? > > I'm really not clear what you're assigning to the template, or how > you're showing it. > Mark, gettype($fee) before settype() gave me 'object'. I'm doing all this in the php and sending it to the template using echo. Initially I was sending to the template with a $smarty->assign('variable', sprintf("%01.2f",$fee2)); Hope trhat's a bit clearer. George |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
George Pitcher wrote:
>>> The following code: >>> >>> $fee = 160.44; >>> $fee2 = $fee + 0; // I have a reason for adding zero; >>> echo $fee."<br>"; >>> echo $fee2."<br>"; >>> settype($fee, "float"); >>> echo gettype($fee); >>> > gettype($fee) before settype() gave me 'object'. > It shouldn't have! Is this a complete code example, in PHP (ie not in Smarty)? On my server the following PHP code: <?php $fee = 160.44; $fee2 = $fee + 0; // I have a reason for adding zero; echo $fee."<br>"; echo $fee2."<br>"; echo gettype($fee)."<br>"; echo gettype($fee2)."<br>"; ?> gives: 160.44 160.44 double double ... as I would have expected. I think you need to resolve this first, but I'm almost certain the problem is that the code example you've posted is not identical to the code you're trying. > I'm doing all this in the php and sending it to the template using echo. > I'm not sure what you mean by "sending it to the template using echo". It sounds like you're sending it to the browser using echo, bypassing the template altogether. A code example would . > Initially I was sending to the template with a $smarty->assign('variable', > sprintf("%01.2f",$fee2)); > I would go with Messju's suggestion, ie use: $smarty->assign('variable',$fee2); and in the template put: {"%.2f"|sprintf:$variable} -- Mark Rogers More Solutions Ltd :: 0845 45 89 555 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Mark,
> George Pitcher wrote: > >>> The following code: > >>> > >>> $fee = 160.44; > >>> $fee2 = $fee + 0; // I have a reason for adding zero; > >>> echo $fee."<br>"; > >>> echo $fee2."<br>"; > >>> settype($fee, "float"); > >>> echo gettype($fee); > >>> > > gettype($fee) before settype() gave me 'object'. > > > > It shouldn't have! > > Is this a complete code example, in PHP (ie not in Smarty)? > > On my server the following PHP code: > <?php > $fee = 160.44; > $fee2 = $fee + 0; // I have a reason for adding zero; > echo $fee."<br>"; > echo $fee2."<br>"; > echo gettype($fee)."<br>"; > echo gettype($fee2)."<br>"; > ?> > gives: > 160.44 > 160.44 > double > double > > .. as I would have expected. > > I think you need to resolve this first, but I'm almost certain the > problem is that the code example you've posted is not identical to the > code you're trying. > > > I'm doing all this in the php and sending it to the template using echo. > > > > I'm not sure what you mean by "sending it to the template using echo". > It sounds like you're sending it to the browser using echo, bypassing > the template altogether. A code example would . > > > Initially I was sending to the template with a > $smarty->assign('variable', > > sprintf("%01.2f",$fee2)); > > > > I would go with Messju's suggestion, ie use: > $smarty->assign('variable',$fee2); > and in the template put: > {"%.2f"|sprintf:$variable} Firstly, the variable might have a price or it might have an explantion, depending on the web-service response. If I run the code that I have described as a pure php file, I get the same result as you - all is fine. but when I run it, and I have, when the output is using a Smarty template, I get the 160.00 result and the 'NULL' gettype. 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. Cheers George |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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? greetings messju > Cheers > > George |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
![]() |
| Outils de la discussion | |
|
|