Forum Moderators: coopster

Message Too Old, No Replies

Help with calculation

         

October

7:09 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Hi! I'm a newbie to php.

I'm creating an order form whereby result is displayed in the next page.

<input type="radio" name="gift" value="toy A"/>
<input type="radio" name="gift" value="toy B"/>
<input type="radio" name="gift" value="toy C"/>

They cost $5, $10 and $15 respectively.

<input type="checkbox" name="extras[]" value="Ribbon" />
<input type="checkbox" name="extras[]" value="Card" />
<input type="checkbox" name="extras[]" value="Wrapper" />
<input type="checkbox" name="extras[]" value="Box" />

Additional $1 for each of the extras checked.

How can I know which toy is chosen, which extras are selected and total up the cost? I just need to display the final total cost.

Will appreciate any help. Thanks!

dreamcatcher

7:34 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi October, welcome to Webmaster World. :)

Your additional items are stored in the $_POST['extras'] array, so to get the cost you can count the slots the array has when the form is submitted. If there are none checked, the count will be 0.

if (!empty($_POST['extras']))
{
echo count [uk.php.net]($_POST['extras']);
}

To display what was checked, implode [uk.php.net] the array.

So, like this:


if (!empty($_POST['extras']))
{
echo "You have chosen <b>".implode(",",$_POST['extras'])."</b> at an additional cost of $".count($_POST['extras']).";
}

Hope that helps.

dc

October

7:53 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Thanks for replying!

Sorry if my question isn't clear. For example, I choose toy B($10) and select two extras($2). How do I display the final total value as $12?

I tried to do a loop, add 1 for every extra selected. But I can't figure out how to get the toy price.

mcibor

8:24 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would create an array containing the price:

$price = array('toy A'=>5, 'toy B'=>10, 'toy C'=>15, 'extra'=>1);

if(in_array [php.net]($_POST['gift'], $price))
$cost = $price[$_POST['gif']];
else $error = "Wrong toy, or whatever error you like";
if (!empty($_POST['extras']))
{
$cost += count($_POST['extras']) * $price['extra'];
}

Regards
Michal

October

9:37 am on Apr 4, 2007 (gmt 0)

10+ Year Member



If I add in this: echo "Cost: $$cost"

The value shown is for the extras. Where do I add in value for the toy? Or do I have to create a new variable that sum up both the cost for toy and extra?

cameraman

9:52 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing that to:
echo "Cost: $cost"

mcibor

2:16 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just saw a thread where there is a problem with in_array.
But I made an error in $_POST, it should certainly be gift, not gif... ;) Sorry:

$price = array('toy A'=>5, 'toy B'=>10, 'toy C'=>15, 'extra'=>1);

if(in_array($_POST['gift'], $price))
$cost = $price[$_POST['gift']];
else $error = "Wrong toy, or whatever error you like";
if (!empty($_POST['extras']))
{
$cost += count($_POST['extras']) * $price['extra'];
}

I think that $$ was a spelling mistake, cause he got something out of it.

dreamcatcher

3:01 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$$ is a variable variable.

dc

October

4:05 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I think there is some problem with the in_array. Only the extras are counted in the final cost, no toy price.

Anyway thanks to everyone for the suggestions!

cameraman

5:44 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$$ is a variable variable but there aren't any in use here. I suspect what he got when he asked for it was the nth index into the hash table but that behaviour could be implementation/version dependent and in effect undefined.

in_array() checks the values, not the keys. So this line:
if(in_array($_POST['gift'], $price))

should be:
if(array_key_exists($_POST['gift'],$price))

October

2:40 am on Apr 5, 2007 (gmt 0)

10+ Year Member



It works perfectly now. Thanks a lot!

Just to clear up the $$ part, I was actually trying to display the currency symbol ($), but I forgot to include the escape character. Since it still works, I didn't notice the mistake till you guys pointed it out.

Thanks again for all the help!