Hi
I could really do with some help here :)
I am tring to display all of the results from the array without having to write everything out by hand like this -
echo get_currency(1, GBP);
-
echo "<br>";
-
echo get_currency(1, USD);
-
echo "<br>";
-
echo get_currency(2, NZD);
-
echo "<br>";
-
I have been trying to use a for each loop but the array name ($aExchangeRates) is returned as undefined. -
foreach ($prices as $key => $value)
-
echo $key.'=>'.$value.'<br />';
-
First goal is to display contents of array.
Second step...try to put those values into function to return $result :)
tyvm to anyone who takes pity on a noob -
-
<?php
-
-
-
define('NZD', 'english-nz');
-
define('GBP', 'english-uk');
-
define('USD', 'american-english');
-
-
function get_currency($iPrice, $original_currency)
-
{
-
-
//-------Associative Array--------------------------//
-
$aExchangeRates = array (
-
-
//-------values in relation to the US dollar--------//
-
-
'GBP' => '0.552272',
-
'USD' => '1',
-
'NZD' => '1.48695'
-
-
);
-
-
setlocale(LC_MONETARY, $original_currency);
-
-
//-----localeconv()-----//
-
//-----Returns an associative array containing localized numeric and monetary formatting information-----//
-
$converting_currency = localeconv();
-
-
$iExchangeRate = $aExchangeRates[trim($converting_currency['int_curr_symbol'])];
-
$iTotal = $iPrice * $iExchangeRate;
-
-
$result = $converting_currency['currency_symbol'] . number_format($iTotal, 5);
-
-
return $result;
-
-
}
-
-
echo get_currency(1, GBP);
-
echo "<br>";
-
echo get_currency(1, USD);
-
echo "<br>";
-
echo get_currency(2, NZD);
-
echo "<br>";
-
-
-
?>
-
11 2045 Atli 5,058
Recognized Expert Expert
Hi.
Are you trying to replace the echo statements at the bottom?
Then the problem is most likely that you are defining your array inside a function, and then trying to use that array outside the function.
Anything created inside a function will only exists inside that function, unless it is somehow made to exist elsewhere. (via the return keyword or something like that).
Try creating your array outside the function, and import it into the function if you need it.
Sorry I made a mistake with this reply and don't know how to delete it :)
Hi.
Are you trying to replace the echo statements at the bottom?
Then the problem is most likely that you are defining your array inside a function, and then trying to use that array outside the function.
Anything created inside a function will only exists inside that function, unless it is somehow made to exist elsewhere. (via the return keyword or something like that).
Try creating your array outside the function, and import it into the function if you need it.
Tyvm :)
I decleared the array outside of the function and then decleared a global var inside the function. Not sure if this is the best wat to do it but it works.
I have the Foreach loop working well but am having trouble calling and executing the function in the Foreach loop.
Goal:
Loop through array and apply function to the values and then output the result. -
foreach($aExchangeRates as $key => $value)
-
{
-
echo get_currency($money, "What goes here"). "<br>";
-
}
-
-
Any help would be awesome
cheers -
<?php
-
-
//-------Associative Array--------------------------//
-
$aExchangeRates = array (
-
//-------values in relation to the US dollar--------//
-
'GBP' => '0.552272',
-
'USD' => '1',
-
'NZD' => '1.48695'
-
);
-
$money = 10;
-
-
//-------Constants--------------------------//
-
define('NZD', 'english-nz');
-
define('GBP', 'english-uk');
-
define('USD', 'american-english');
-
-
function get_currency($iPrice, $original_currency)
-
{
-
setlocale(LC_MONETARY, $original_currency);
-
-
//-----localeconv()-----//
-
$converting_currency = localeconv();
-
global $aExchangeRates;
-
-
$iExchangeRate = $aExchangeRates[trim($converting_currency['int_curr_symbol'])];
-
$iTotal = $iPrice * $iExchangeRate;
-
-
$result = $converting_currency['currency_symbol'] . number_format($iTotal, 5);
-
-
return $result;
-
-
}
-
echo get_currency($money, GBP). "<br>";
-
echo "<br>";
-
echo get_currency($money, USD). "<br>";
-
echo "<br>";
-
-
foreach($aExchangeRates as $key => $value)
-
{
-
echo $key . " - ". $value . "<br>";
-
}
-
?>
-
-
Atli 5,058
Recognized Expert Expert
If I'm reading this correctly, you would pass the $value of the foreach loop into the second parameter of your function.
That would be the name of the locale you want to convert into.
The first parameter would be the dollar amount you want converted.
If I'm reading this correctly, you would pass the $value of the foreach loop into the second parameter of your function.
That would be the name of the locale you want to convert into.
The first parameter would be the dollar amount you want converted.
Thats correct. Have tried -
foreach($aExchangeRates as $key => $value)
-
{
-
echo get_currency($money, $iExchangeRate). "<br>";
-
}
-
but get this error message
Notice: Undefined variable: iExchangeRate in C:\Program Files\Apache\htdocs\currency2.php on line 37
$14.86950 -
Line 37 is
-
echo get_currency($money, $iExchangeRate). "<br>";
-
I have tried making $iExchangeRate global but no joy. If function logic is - $iTotal = $iPrice * $iExchangeRate;
Converted currency = $money * $iExchangeRate
any thoughts please :)
Atli 5,058
Recognized Expert Expert
You loop through the $aExchangeRates array so you can use each value from it.
So you should be passing the $value of the foreach loop into the function call: -
foreach($aExchangeRates as $value)
-
{
-
echo get_currency($money, $value). "<br>";
-
}
-
And there is no need for the $key part you added, so I removed that.
Check out foreach loops in the manual if this isn't making sense.
You loop through the $aExchangeRates array so you can use each value from it.
So you should be passing the $value of the foreach loop into the function call: -
foreach($aExchangeRates as $value)
-
{
-
echo get_currency($money, $value). "<br>";
-
}
-
And there is no need for the $key part you added, so I removed that.
Check out foreach loops in the manual if this isn't making sense.
I have reread the manual. -
/*example 1 - Basic loop thru assoc array*/
-
foreach($aExchangeRates as $key => $value)
-
{
-
echo $key . " - ". $value . "<br>";
-
-
}
-
-
/*example 2 - looping values and passing thru function*/
-
foreach($aExchangeRates as $value)
-
{
-
echo get_currency($money, $value). "<br>";
-
}
-
example 1 - works fine
example 2 - gives this error message
Notice: Undefined index: in C:\Program Files\Apache\htdocs\currency2.php on line 25
0.00000
my understanding of this error is : referring to an array element that does not exist.
Example 1 - works and uses $value so it exists???
I can see now that $value is the right value to pass thru the function. I just don't know how to reference it in example 2.
I'm such a moron :) I really appreciate your help and have tried everything I know. I just don't know where to go from here. -
<?php
-
-
//-------Associative Array--------------------------//
-
$aExchangeRates = array (
-
//-------values in relation to the US dollar--------//
-
'GBP' => '0.552272',
-
'USD' => '1',
-
'NZD' => '1.48695'
-
);
-
$money = 10;
-
-
//-------Constants--------------------------//
-
define('NZD', 'english-nz');
-
define('GBP', 'english-uk');
-
define('USD', 'american-english');
-
-
function get_currency($iPrice, $original_currency)
-
{
-
setlocale(LC_MONETARY, $original_currency);
-
-
//-----localeconv()-----//
-
$converting_currency = localeconv();
-
global $aExchangeRates;
-
-
$iExchangeRate = $aExchangeRates[trim($converting_currency['int_curr_symbol'])];
-
$iTotal = $iPrice * $iExchangeRate;
-
-
$result = $converting_currency['currency_symbol'] . number_format($iTotal, 5);
-
-
return $result;
-
-
}
-
-
-
-
/*Basic loop thru assoc array*/
-
foreach($aExchangeRates as $key => $value)
-
{
-
echo $key . " - ". $value . "<br>";
-
-
}
-
-
/*looping values and passing thru function*/
-
foreach($aExchangeRates as $value)
-
{
-
echo get_currency($money, $value). "<br>";
-
}
-
-
?>
-
Atli 5,058
Recognized Expert Expert
Ok. The undefined index problem isn't referring to the foreach loop now.
It's all that locale stuff in your function that isn't working. The locale isn't being set properly, so when you try to read the values based on the set locale, they don't exists and you get this warning.
Why do you have all this locale stuff there?
Is it just to get the correct currency symbol?
It would be much easier to just add that to your exchange rate array as well, to avoid these sort of problems.
Ok. The undefined index problem isn't referring to the foreach loop now.
It's all that locale stuff in your function that isn't working. The locale isn't being set properly, so when you try to read the values based on the set locale, they don't exists and you get this warning.
Why do you have all this locale stuff there?
Is it just to get the correct currency symbol?
It would be much easier to just add that to your exchange rate array as well, to avoid these sort of problems.
I took your great advice and simplified the code :) Yay it works........
one small question. This is returned:
Exchange Rates USD
GBP - 0.552272
USD - 1
NZD - 1.48695
10 US Dollars buys you:
NZD - 5.52272
NZD - 10.00000
NZD - 14.86950
I have tried putting in $key to show currency type but it is taking last value and applying it to all.
any ideas - then i'll go away
ty so much for your help and advice. I've learnt a LOT -
<?php
-
//-------Associative Array--------------------------//
-
$aExchangeRates = array (
-
//-------values in relation to the US dollar--------//
-
'GBP' => '0.552272',
-
'USD' => '1',
-
'NZD' => '1.48695'
-
);
-
$money = 10;
-
-
function get_currency($iPrice, $original_currency)
-
{
-
$iTotal = $iPrice * $original_currency;
-
$result = number_format($iTotal, 5);
-
return $result;
-
}
-
-
echo "Exchange Rates USD"."<br>";
-
/*Basic loop thru assoc array*/
-
foreach($aExchangeRates as $key => $value)
-
{
-
echo $key . " - ". $value . "<br>";
-
}
-
-
echo "<br>";
-
echo $money." US Dollars buys you:"."<br>";
-
-
/*looping values and passing thru function*/
-
foreach($aExchangeRates as $value)
-
{
-
echo $key." - ". get_currency($money, $value)."<br>";
-
}
-
-
?>
-
Atli 5,058
Recognized Expert Expert
Glad you got it to work :)
As for the problem. Your second loop doesn't set a $key value, so the last $key from your first loop is being used throughout the entire second loop.
You need to have your second loop set the $key value, like your fist loop does.
Glad you got it to work :)
As for the problem. Your second loop doesn't set a $key value, so the last $key from your first loop is being used throughout the entire second loop.
You need to have your second loop set the $key value, like your fist loop does.
Cheers
Everything is working as it should :)
ty so much. I really appreciate your patience and help.
Loz
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Abdullah Kauchali |
last post by:
Hi folks,
Can one rely on the order of keys inserted into an associative Javascript
array? For example:
var o = new Object();
o = "Adam";
o = "Eve";
|
by: mark4asp |
last post by:
Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. . Eg.
because the key difference...
|
by: JGH |
last post by:
How can I check if a key is defined in an associative array?
var users = new array();
users = "Joe Blow";
users = "John Doe";
users = "Jane Doe";
function isUser (userID)
{
if (?????)
|
by: Robert |
last post by:
I am curious why some people feel that Javascript doesn't have
associative arrays. I got these definitions of associative arrays via
goggle:
Arrays in which the indices may be numbers or...
|
by: Derek Basch |
last post by:
Is there any way to associate name/value pairs during an array
initialization? Like so:
sType = "funFilter"
filterTypeInfo = ;
filterTypeInfo = new Array("type" : sType);
I can do it using...
| |
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they...
|
by: VK |
last post by:
Whatever you wanted to know about it but always were affraid to ask.
<http://www.geocities.com/schools_ring/ArrayAndHash.html>
|
by: Robert Mark Bram |
last post by:
Hi All!
How do you get the length of an associative array?
var my_cars= new Array()
my_cars="Mustang";
my_cars="Station Wagon";
my_cars="SUV";
alert(my_cars.length);
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |