help calculating total from option boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

help calculating total from option boxes

Post by dthomas31uk »

I have 3 option boxes

Code: Select all

echo 'Load from 14m3 - 20m3' . "<label><input type='radio' name='load' value='luton'</label>"."<BR>";
 
echo 'Load from 7m3 - 14m3'."<label><input type='radio' name='load' value='full_load'</label>"."<BR>";
 
echo 'Load from 1m3 - 7m3' . "<label><input type='radio' name='load' value='half_load'</label>"."<BR>"; 
 
And need to calculate totals based on one of the options selected. Trouble is I think my if statements are not written correctly. Can anyone advise here is the code that calcultes the option boxes from my database

Code: Select all

if ($load == 'full_load') {
 
  $total = $row['full_price'];  
 
} 
 
if  ($load == 'half_price') {
 
  $total = $row['half_price'];
 
}
 
if  ($load == 'luton') {
 
  $total = $row['luton'];
 
} 
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: help calculating total from option boxes

Post by papa »

You could use a switch:

Code: Select all

 
$load = $_POST['load'] //Depending on your form method
 
switch($load) {
 case "full_load":
$total = $row['full_price'];  
 break;
 case "half_price":
  $total = $row['half_price'];
 break;
 case "luton":
$total = $row['luton'];
 break;
}
 
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

Re: help calculating total from option boxes

Post by dthomas31uk »

Hi papa. had it working when I had two option buttons like so

Code: Select all

if ($load == 'full_load') {
  $total = $row['full_price'];
  
} else {
  $total = $row['half_price'];
}
 
have tried your switch code, but it only calculates the 'half_price' , dont know why
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: help calculating total from option boxes

Post by aceconcepts »

You haven't ended your input elements:

Change

Code: Select all

<input type='radio' name='load' value='half_load'
 
To

Code: Select all

<input type='radio' name='load' value='half_load' />
Forgot to mention, you need to do this to all of your radio elements.
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

Re: help calculating total from option boxes

Post by dthomas31uk »

have got it working now Papa cheers. The problem was that I did not select my third option in my select query, thus it did not calculate. Thanks a lot
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: help calculating total from option boxes

Post by papa »

No probs. Good it worked out for you!
Post Reply