Jump to content

[SOLVED] session syntax?


NickG21

Recommended Posts

this is my first time using sessions and i think that i am pretty close to having this right.  i am unable to reset my CurrencyID when the image is clicked on in my form.  does anyone know where i am going wrong with this?

[code]<?php
$CurrencySymbols = array( 1 => "\$", 2 => "€", 3 => "£");
//Examples Made Up
$CurrencyExchangeRate = array(1 => 1, 2 => 0.759594, 3 => 0.510553);

$_SESSION["CurrencyID"] = isset($_SESSION["CurrencyID"]) ? $_SESSION["CurrencyID"] : 1;

$ItemPrice = 80;

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

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

<html>
<body>
<form action="converter.php" method="GET">
<a href="converter.php?currencyID=1"><img src="./usd.jpg" align="left" border="0"></a>
<a href="converter.php?currencyID=2"><img src="./euro.jpg" align="left" border="0"></a>
<a href='".$_SERVER['PHP_SELF']."?currencyID=3'>British Sterling</a><br />
</form>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32174-solved-session-syntax/
Share on other sites

You don't really need those form HTML tags... though they might just be hanging around from your real code... also I'd like to remind you that since you are setting the $_SESSION['CurrencyID'] AFTER the logic... it won't be set until the script is run again...

if it's converter.php?currencyID=2 it will still display USD for one run (will be euro if you refreshed the page) since you don't change the value of $_SESSION['CurrencyID'] until after you do your...
"echo $CurrencySymbols[$_SESSION["CurrencyID"]].$CurrencyPrice;"
Link to comment
https://forums.phpfreaks.com/topic/32174-solved-session-syntax/#findComment-149367
Share on other sites

try that:
[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="?currencyID=1"><img src="./usd.jpg" align="left" border="0" alt="USD" /></a><br />
<a href="?currencyID=2"><img src="./euro.jpg" align="left" border="0" alt="Euro" /></a><br />

<a href="<?php echo $_SERVER['PHP_SELF']; ?>?currencyID=3">British Sterling</a><br />

</body>
</html>[/code]
You had some of your code in the wrong place.
Link to comment
https://forums.phpfreaks.com/topic/32174-solved-session-syntax/#findComment-149374
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.