views:

63

answers:

3

Hi Gang,

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 ????

+2  A: 

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;
Ewan Todd
OMG. Duhhh.. thanks Ewan. ;)
rob - not a robber
A: 

That's quite an uncommon way of handling post data.

You should not hard code your prices into your code! Instead let the form know only a productID and store the information about your products in a db or similar.

So if I select the 'dog' the value of the option will be the productID of the dog-product and this product ID will be used in a db query to retrieve the necessary information about the dog.

tharkun
tharkun,Thanks, i realize that option is most efficient, but like I mentioned, I walked into this project with many inefficiencies present.THANKS!
rob - not a robber
+1  A: 

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.

Ivan Nevostruev
Thanks Ivan... surely a cool method. I feel your logic
rob - not a robber