• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 If and elseif Question El Forum Guest   09-06-2008, 03:34 PM [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 El Forum Guest   09-06-2008, 03:57 PM [eluser]xwero[/eluser] Code:```\$event_cost = 25; for(\$i=1;\$i<=4;\$i++) {    if(\$_POST['guest_name_'.\$i] != '')    {        \$event_cost += 25;    } }``` El Forum Guest   09-06-2008, 04:00 PM [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? El Forum Guest   09-06-2008, 04:06 PM [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! El Forum Guest   09-06-2008, 05:36 PM [eluser]Chris Newton[/eluser] Or use the validation class to trim before you even get to the post processing. El Forum Guest   09-06-2008, 06:55 PM [eluser]Jesse Schutt[/eluser] Oh yes. I will give that a shot. I hadn't been validating those fields as they aren't required.