Welcome Guest, Not a member yet? Register   Sign In
If and elseif Question
#1

[eluser]Jesse Schutt[/eluser]
Hello All,

I am building a simple registration program that is for a Father/Son retreat. The registration cost is 25 dollars per person. The form is to be filled out by the father. Under his information are four fields for additional "guests" or sons. What I am trying to do is calculate the amount of money that should be transferred to PayPal for the total bill.

Could you look at this and let me know if this is the best way to do it?


Code:
if($this->input->post('guest_name_4') != '')
{
      $event_cost = 125;
}
elseif($this->input->post('guest_name_3') != '')
{
      $event_cost = 100;
}
elseif($this->input->post('guest_name_2') != '')
{
      $event_cost = 75;
}
elseif($this->input->post('guest_name_1') != '')
{
      $event_cost = 50;
}


This does work, but I am sure there is a better way to do it...

Thanks in advance!

Jesse
#2

[eluser]xwero[/eluser]
Code:
$event_cost = 25;
for($i=1;$i<=4;$i++)
{
   if($_POST['guest_name_'.$i] != '')
   {
       $event_cost += 25;
   }
}
#3

[eluser]Jesse Schutt[/eluser]
Hah! I knew there would be a better way to do it!

What about if the user accidentally types a space into the 4th guest space and is only bringing one guest?
#4

[eluser]Jesse Schutt[/eluser]
This right?

Code:
$event_cost = 25;
for($i=1;$i<=4;$i++)
{
   if(trim($_POST['guest_name_'.$i]) != '')
   {
       $event_cost += 25;
   }
}

Thanks for helping me out!
#5

[eluser]Chris Newton[/eluser]
Or use the validation class to trim before you even get to the post processing.
#6

[eluser]Jesse Schutt[/eluser]
Oh yes. I will give that a shot. I hadn't been validating those fields as they aren't required.




Theme © iAndrew 2016 - Forum software by © MyBB