Jump to content

little help needed


NickG21

Recommended Posts

below i have my script that i have been working on all day for the currency conversion.  it works properly thanks to all the help of the people on these forums, right now it only changes the price of the one value entered into $itemprice.  is there anyway i can set this up to be able to change multiple values on one page, since this is going to be a pricing page and there will be much more than just one price on it.

[code]<?php
session_start();

if(!isset($_GET["currencyID"]))
{
    $_SESSION["CurrencyID"] = isset($_SESSION["CurrencyID"]) ? $_SESSION["CurrencyID"] : 1;

}
elseif(isset($_GET["currencyID"]))
{
    if(is_numeric($_GET["currencyID"]))
    {
        $_SESSION["CurrencyID"] = $_GET["currencyID"];
    }
}

$CurrencySymbols = array( 1 => "\$",
                          2 => "€",
                          3 => "£"
                        );
//Examples Made Up
$CurrencyExchangeRate = array( 1 => 1,
                              2 => 0.759594,
                              3 => 0.510553
                              );

$ItemPrice = 80;

$CurrencyPrice = $ItemPrice * $CurrencyExchangeRate[$_SESSION["CurrencyID"]];

echo $CurrencySymbols[$_SESSION["CurrencyID"]].$CurrencyPrice;


?>

<html>
<body>

<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=1"><img src="./usd.jpg" align="left" border="0" alt="USD" /></a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=2"><img src="./euro.jpg" align="left" border="0" alt="Euro" /></a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=3"><img src="./pound.jpg" align="left" border="0" alt="Pound"/></a>

</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32200-little-help-needed/
Share on other sites

Yoo will want to store each item in an array, call this array items. this array will store the item name and price. Then you will want to use a foreach loop to display all the items. Example:
[code]<?php
session_start();

function getPrice($price, $dec=2)
{
    if(!isset($_GET["currencyID"]))
    {
        $_SESSION["CurrencyID"] = isset($_SESSION["CurrencyID"]) ? $_SESSION["CurrencyID"] : 1;

    }
    elseif(isset($_GET["currencyID"]))
    {
        if(is_numeric($_GET["currencyID"]))
        {
            $_SESSION["CurrencyID"] = $_GET["currencyID"];
        }
    }

    $CurrencySymbols = array( 1 => "\$",
                              2 => "€",
                              3 => "£"
                            );
    //Examples Made Up
    $CurrencyExchangeRate = array( 1 => 1,
                                  2 => 0.759594,
                                  3 => 0.510553
                                  );

    $price = $price * $CurrencyExchangeRate[$_SESSION["CurrencyID"]];

    $price = number_format($price, $dec);

    return $CurrencySymbols[$_SESSION["CurrencyID"]].$price;
}


// Array of items:
//                  item name      , price
$items= array( array('Candy Cane'    , 0.75),
              array('Ginger Bread'  , 2.50),
              array('Cristmas Cake' , 5.60)
              );

// loop through each item and display price:
foreach($items as $item)
{
    echo $item[0] . ' : ' . getPrice($item[1], 2);
    echo '<br /><br />';
}

?>

<html>
<body>


Show prices in:
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=1"><img src="./usd.jpg" align="left" border="0" alt="USD" /></a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=2"><img src="./euro.jpg" align="left" border="0" alt="Euro" /></a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=3"><img src="./pound.jpg" align="left" border="0" alt="Pound"/></a>

</body>
</html>[/code]
I created a function called getPrice to save having to write out the code in the foreach loop.
Link to comment
https://forums.phpfreaks.com/topic/32200-little-help-needed/#findComment-149549
Share on other sites

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.