Click to See Complete Forum and Search --> : Define variable dependent on conditional statement
bryndog
04-15-2005, 07:15 AM
Hey,
I'm new to this webmonkery so I wondered if yanyone could help me out. I`m learning php, and i need to define a $price variable dependent upon the information received from the previous page. The code I am using at the moment is this:
--------------------------------------------------------------------------
<?php
echo "<p>Thank you for your selection<?p>";
//conditional on what selection has been made
if ($pack == 'starter'){
echo '<p>You have chosen the Starter Pack priced at 129.99!</p>';
} else if ($pack == 'visual'){
echo '<p>You have chosen the Visuals Only pack priced at 99.99</p>';
} else if ($pack == 'promotion'){
echo '<p>You have chosen the Promotion Only pack priced at 49.99</p>';
} else {
echo '<p>You have not made a selection from the options available.</p>';
}
?>
-------------------------------------------------------------------------
This is currently working just fine. But I also need to define a $price variable based on the user selection that come through. I just dont know the proper syntax to do it.
Help!
Bryndog
abectech
04-15-2005, 07:38 AM
<?php
echo "<p>Thank you for your selection<?p>";
//conditional on what selection has been made
if ($pack == 'starter'){
echo '<p>You have chosen the Starter Pack priced at 129.99!</p>';
$price = "$129.99";
} else if ($pack == 'visual'){
echo '<p>You have chosen the Visuals Only pack priced at 99.99</p>';
$price = "$99.99";
} else if ($pack == 'promotion'){
echo '<p>You have chosen the Promotion Only pack priced at 49.99</p>';
$price = "49.99";
} else {
echo '<p>You have not made a selection from the options available.</p>';
}
?>
OR because I have heard Case/Switch is faster to process...
How are you getting $pack to the second page?
<?php
echo "<p>Thank you for your selection<?p>";
//conditional on what selection has been made
switch ($pack) {
case "starter":
echo "<p>You have chosen the Starter Pack priced at 129.99!</p>";
$price = "$129.99";
break;
case "visual":
echo "<p>You have chosen the Visuals Only pack priced at 99.99</p>";
$price = "$99.99";
break;
case "promotion":
echo "<p>You have chosen the Promotion Only pack priced at 49.99</p>";
$price = "$49.99";
break;
default:
echo "<p>You have not made a selection from the options available.</p>";
}
?>
bryndog
04-15-2005, 08:18 AM
Hey,
Thanks for the post.
I knew it would be something simple.
In answer to your question, '$pack' is being sent from the previous page via a form with 3 radio buttons, using the post method.
Thanks again,
Bryndog
NogDog
04-15-2005, 08:25 AM
Sometimes I like to approach this sort of thing with a data array. This way I only have to change the contents of the array if I need to add, delete, or edit the choices:
<?php
$packData = array (
"starter" => "Starter Pack:129.99",
"visual" => "Visuals Only pack:99.99",
"promotion" => "Promotion Only pack:49.99"
);
if(array_key_exists($pack, $packData))
{
list($description, $price) = explode(":", $packData[$pack]);
echo "<p>You have chosen the $description priced at $price!</p>\n";
}
else
{
echo "<p>You have not made a selection from the options available.</p>\n";
}
?>
abectech
04-15-2005, 10:18 AM
If you're getting it via post I would suggest using $_POST['pack'] or $_REQUEST['pack'] instead of just $pack... For me it makes it easier to look at the code later and figure out what is going on if need be. I have also heard that in some instances $pack wont always work but I have never ran into this.
Does anyone know why it may not work in some instances? Or am I full of it?
NogDog
04-15-2005, 03:47 PM
If you're getting it via post I would suggest using $_POST['pack'] or $_REQUEST['pack'] instead of just $pack... For me it makes it easier to look at the code later and figure out what is going on if need be. I have also heard that in some instances $pack wont always work but I have never ran into this.
Does anyone know why it may not work in some instances? Or am I full of it?
If register_globals is turned off in your PHP configuration (which it is by default from PHP 4.2.0 and later), then you must use the $_POST['name'] method to reference form values. Since this method works regardless of whether register_globals is on or off, it's the safest and most portable way to go.