Forum Moderators: coopster

Message Too Old, No Replies

setlocale LC MONETARY for multiple variables

         

smallcompany

11:54 pm on Sep 17, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm running a website that shows prices for multiple products - in multiple countries. I use PHP to store prices and calculate discounts which are all shown on corresponding pages by echoing them with PHP.

Now, I see that a local currency can be set in this way:

$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);


This is fine, but what if I have multiple values, like $number1, $number2, $number3, $number3, $number4 and so on. I should not set a locale each time for each variable I hope.
I do echo variables in the final HTML pages, but how do I set the locale prior that - for several variables at once?

Thank you

lucy24

12:49 am on Sep 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



php docs [php.net] do say, rather terrifyingly,
The locale information is maintained per process, not per thread. ... you may experience sudden changes in locale settings while a script is running
but honestly I think they're just trying to scare you. This is all happening within the same function, right? It's not as if your php is going to wander off, come back 5 minutes later and now you're in a whole new locale.

smallcompany

5:32 am on Sep 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Huh, thanks.

Well, I have a single PHP file where I maintain prices and calculate discounts. That file is included into HTML template. This means every page load would load variables that state full and discounted prices, plus the value of a discount, being a money value or a percentage.
Each page that uses this is a single currency page, that is, I may have six different offers with three values for each, becoming 18 values on the page, all same currency.

Now, heck, what here is a single process if each of these values on a page is called in this way:
<?php echo $dk11;?>

and now using that money thing from PHP, it became this:
<?php setlocale(LC_MONETARY,'da_DK');echo money_format("%n",$dk11);?>


I'm trying not to do that setlocale and what follows 18 times on the page, but call the variable only, $dk11 in this example. The price maintenance file would look like this:

// DK full prices
$dk11f = "499";
$dk12f = "999";
$dk21f = "699";
$dk22f = "1399";
// DK prices
$dk11 = "369";
$dk12 = "899";
$dk21 = "519";
$dk22 = "1299";
// DK discounts
$dk11d = $dk11f - $dk11;
$dk12d = $dk12f - $dk12;
$dk21d = $dk21f - $dk21;
$dk22d = $dk22f - $dk22;


Is it possible to somehow wrap the above code, and apply that setlocale once, not for each line - the question is now?
If possible, I assume I could continue using the on page calls in simple way, like
<?php echo $dk11;?>


Thank you

whitespace

12:54 am on Sep 20, 2018 (gmt 0)

10+ Year Member Top Contributors Of The Month



...you may experience sudden changes in locale settings while a script is running


Although in general usage you are often only calling it once per request, near the start of the script. Or perhaps multiple times within the same script if you are displaying multiple locales on the same page? I don't think this should be causing a problem here?

Each page that uses this is a single currency page


So, you should only need to call setlocale() once per request - at the top of the script - for whichever locale you are displaying on the page.

However, you will need to call money_format() (or wrap it in another function) everytime you want to "display" a money value in the output. You shouldn't simply echo the variable directly (as you seem to imply), unless you really don't care about the output, or you have already assigned the formatted money value to that variable? But you can minimise it to something like:


<?=getMoney($dk11)?>


You then write a getMoney() function that returns the formatted value.


$dk11 = "369";


However, an issue here is that you are storing the money values as "strings", not as numbers. When PHP later does arithmetic on these "strings", it's going to have to do some type conversion, which is prone to error. Is there a reason why strings are used here?

Also, it looks like it would be a lot easier to manage (from a programming point of view) if you stored these values in an array-like structure, rather than individual variables. You could then "loop over" the array to perform your calculations and avoid repetition. (You don't necessarily need to store the "discounts", they could perhaps be calculated as required, eg. getDiscount('DK','11') ?) Is there a reason why individual variables are used here?

smallcompany

2:04 am on Sep 24, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you. After many hours, I was able to create a proper function that turns numbers into a monetary value for each region. The last part of the suggestion to calculate discounts on requests is a challenge. This is how far I went:

As I have full and discounted prices already stored, I tried to do a simple full minus discounted to get the value of the discount - on the fly.

I tried to use eval() to create a variable that matches the product, for its full price.

In HTML file I include this PHP code:
<p><?php echo showDiscount($price11);?></p>

And here is the code that should create a new variable, $price11f:
<?php
include "prices-full.php";
// * Function to calculate discount
function showDiscount($priceVar) {
$fullVar = "$priceVar".'f';
$full = eval('return '.$fullVar.';');
$current = $priceVar;
echo $fullVar . "," . $current . "," . $full;
}
?>


The code above is to echo the outcome. What I get is a character "f" appended to a price, i.e. 39.99.

Both full and discounted prices are in that included prices-full.php file. $price11 (39.99) is shown, but instead of full price, I get 39.99f.

If this works, I would calculate the discount, but it's not working. eval does not turn that string into a variable.

All I'm trying to do is to pull the variable $price11f, based on its pair $price11. Both are stored in a single PHP file.

Thanks