CodeIgniter Forums
If and elseif Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: If and elseif Question (/showthread.php?tid=11388)



If and elseif Question - El Forum - 09-06-2008

[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


If and elseif Question - El Forum - 09-06-2008

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



If and elseif Question - El Forum - 09-06-2008

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


If and elseif Question - El Forum - 09-06-2008

[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!


If and elseif Question - El Forum - 09-06-2008

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


If and elseif Question - El Forum - 09-06-2008

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