0

I have a inefficiently constructed form that is obviously sending one type of data per SELECT element. For simplicity's sake, here's an example.

The form posts data from 2 SELECT elements, but it actually needs to contain 3 pieces of data. To the user, the SELECTs do contain 3 pieces of data; one SELECT denotes quantity and the other SELECT denotes an item name AND a price using text between the OPTION tags.

So, the form is sending only $quantity and $itemname

In processing, I am trying to construct a bunch of IFs or a SWITCH to provide for the missing prices. Being that the string associated with $itemname is unique, I am trying to make a "key" that provides the price. My logic is, if $itemname contains "One Inch Nut" I could find for $price with something like:

if ($itemname = "Once Inch Nut;") { $price = .25; }

And if $itemname were something else, I would add another IF or IF else or CASE in a switch.

In fact, I am doing just that, yet when I select another OPTION from the SELECT, the $price and even the $item name are defined as the last "IF" in my stack of "IFs"

So if I were to select like, the 3rd item in a 20 item SELECT grouping, when it came time for me to use the variable (adding, echoing, whatever) I'd end up with the end of the "IF stack"

Obviously, my logic and understanding of IE ELSEIF ELSE is flawed.

Ideas on how to make this "Key" from a variable's string data?

Thanks, Rob

See below for an abridged example of what I thought would work:

Suppose the form posts "cat" for $itemname and "3" for $qty

<? 
$itemname =  $_POST['specificitem'] ;
$qty =  $_POST['quantity'] ;

if ($itemname = "cat;") { $price = 2.25; }
if ($itemname = "dog;") { $price = 1.25; }
if ($itemname = "hamster;") { $price = 3.25; }
if ($itemname = "parakeet;") { $price = 7.25; }
if ($itemname = "werewolf;") { $price = 122.25; }

echo $itemname; 
echo "$"$price;

?>

Should yield: cat $2.25 ????

1
  • What kind of data would sting data be? Like "Roxanne" and such? Commented Nov 23, 2009 at 19:19

2 Answers 2

2

You need == to test:

if ($itemname == "cat;") { $price = 2.25; }

However, it is common to see switch used for this type of situation:

$itemname =  $_POST['specificitem'] ;
$qty =  $_POST['quantity'] ;

switch($itemname)
{
  case "cat;":
    $price = 2.25;
    break;
  case "dog;":
    $price = 1.25;
    break;
  case "hamster;":
    $price = 3.25;
    break;
  case "parakeet;":
    $price = 7.25;
    break;
  case "werewolf;":
    $price = 122.25;
    break;
}

echo $itemname;
echo "$"$price;
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you to use "table" approch for this code. Create an array representing price by item name. And get price in single line like following:

<? 
$itemname =  $_POST['specificitem'] ;
$qty =  $_POST['quantity'] ;

## creating a price "table"
$item_prices = array(
    'cat;' => 2.25,
    'dog;' => 1.25,
    ## adding more is easy here
);
if (array_key_exists($itemname, $item_prices)) {
    ## get price in single line
    $price = $item_prices[$itemname];
}
else {
    ## set some default price if item is unknown
}

echo $itemname; 
echo "$",$price;

?>

Later you can use $item_prices to generate options for your select tag if you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.