Jump to content

WANT HELP WITH CURRENCY CONVERTER


salman233

Recommended Posts

hello everyone i am newbie to php just wanted to make a script for currency converter the problem i am facing is if i want to convert a value from $usd to $euro,$pond,$yen there are three possible values for usd kindly help me out with designing the values for tables and fetching data from them what will be structure of the table what will be the values kindly help me out it,s request thanks

Link to comment
https://forums.phpfreaks.com/topic/236710-want-help-with-currency-converter/
Share on other sites

OK, so I found these sample rates here:  http://www.x-rates.com/

 

You basically need either an array, or a table in mysql to store the conversion rates.  I think an array would work nicely:

$conversions = array(
    'USD'=>1,
    'GBP'=>1.62343,
    'CAD'=>1.02554
);

 

Having that, you then would neet some way to convert the data.  Probably a function:

function br()
{
echo '<br/>';
}
function converter($need, $have, $value, $conversionArray)
{
($conversionArray[$have] / $conversionArray[$need]) * value;
}
var_dump($conversions);
br();
echo converter('USD', 'GBP', 5.25, $conversions);
br();
echo converter('GBP', 'CAD', 5.25, $conversions);

 

Try it and let me know how it works.

 

 

Sorry, did some error testing at the CLI,

 

This works great:

$conversions = array(
    'USD'=>1,
    'GBP'=>1.62343,
    'CAD'=>1.02554
);
function br()
{
echo '<br/>';
}
function converter($need, $have, $value, $conversionArray)
{
return (($conversionArray[$have] / $conversionArray[$need]) * $value);
}
var_dump($conversions);
br();
echo converter('USD', 'GBP', 5.25, $conversions);
br();
echo converter('GBP', 'USD', 5.25, $conversions);
br();
echo converter('GBP', 'CAD', 5.25, $conversions);

OK,

 

In the example above we created an 'associative array'.  This basically means that we are identifying the values of the array with 'keys' that contain string values.  IE $conversions['USD'] = 1 in our case above.

 

We created a function that accepts 4 arguments as parameters:

function converter($need, $have, $value, $conversionArray)

$need is the key value we want.

$have is the key value we have.

$value is the amount we have.

$conversionArray is the array containing the Keys / values of our conversions.

 

you can read more at http://www.php.net/manual/en/language.types.array.php,

however I recommend you download the files locally for quicker reference:

http://www.php.net/download-docs.php

 

I have a .chm file on my desktop  :D  I love having it there!

I hate to be the jerk, but you could do some of the coding. This is the "Help forum", not the "free labor" forum.  If you give us bad code, we will help you fix it -- gladly.  If you tell us to do all the work, you should be in the freelance section.

 

edit Just noticed the freelance section is gone. Smeh. PM someone, and offer to pay them.

Hey salman, I don't mind being of help.  Jons does have a point though.  Learning these topics will take a lot of time, trial & error, and studying.  You can do it though!

 

I would start off by installing xampp if you haven't already done so,  you can create databases and tables fairly easy with phpmyadmin.

 

Study the mysql functions in the php docs function->database extensions -> mysql reference.

 

Try out some code, read, learn, try some more, when you run into a wall, come back and start a new thread.

 

Good Luck!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.