Any 'other' way to SUM variables using PHP?
In a form I have, a consumer can choose to add a blood test, a vision test, or both (combo) to several enrollee's accounts. This is all done at one time. Based on the choice made, I am able to select the 'choice' and echo a respective price but am having the hardest time building a script to 'add or sum' all the variables together.
I am aware of the array_sum() feature but am either a) not using it correctly, b) does not apply in my case.
A sample of the code I have is:
    <tr>
        <td>Test(s) Ordered:</td>
        <td>
        <?php
            if($_SESSION['F'.$j.'Tests'] == "Blood") {
                $prem = "62.95";
                echo "Blood Test - $".$prem." per month";
            }
            elseif($_SESSION['F'.$j.'Tests'] == "Vision") {
                $prem = "60.00";
                echo "Vision Test - $".$prem." per month";
            }
            elseif($_SESSION['F'.$j.'Tests'] == "BVCombo") {
                $prem = "122.95";
                echo "Blood and Vision - $".$prem." per month";
            }
        ?>
        </td>
Where $j is the number of the enrollee determined in a FOR loop above. This $j variable can range from 1 to 16 and i will never know how many enrollees until the enrollment is complete from person to person.
Ultimately, I am trying to accomplish the following code but cannot figure how to do so:
$sum = $prem1 + $prem2 + $prem3 + $prem4
where $prem1 and $prem2 relate to the specific individuals. Each individual will only have a single premium total so $prem1 may equal $62.95, 60.00, or 122.95 and so on for succeeding enrollees.
**
Final Solution Used
** I first created the $total = array(); variable outside of the FOR loop for开发者_Python百科 my entire form. Then:
        <?php
            if($_SESSION['F'.$j.'Tests'] == "Blood") {
                $prem = "62.95";
                echo "Blood Test - $".$prem." per month";
                $total[] = $prem;
            }
            elseif($_SESSION['F'.$j.'Tests'] == "Vision") {
                $prem = "60.00";
                echo "Vision Test - $".$prem." per month";
                $total[] = $prem;
            }
            elseif($_SESSION['F'.$j.'Tests'] == "BVCombo") {
                $prem = "122.95";
                echo "Blood and Vision - $".$prem." per month";
                $total[] = $prem;
            }
        ?>
Finally, outside of the loop altogether:
<?php 
    } 
    $summ = array_sum($total);
    $premiumtotal = number_format($summ,2,'.','');
    echo "$".$premiumtotal;
?>
Thanks for all the help!
In order to use array_sum you need an array. Store all the parcels in an array, like this:
$prem = array();
while (cond()) {
    //...
    if (cond2())
        $prem[] = 60;
    //...
}
$total = array_sum($prem);
I hope you got the idea.
You could do something like this:
   <tr>
        <td>Test(s) Ordered:</td>
        <td>
        <?php
            if($_SESSION['F'.$j.'Tests'] == "Blood") {
                $prem = "62.95";
                $total += $prem;
                echo "Blood Test - $".$prem." per month";
            }
            elseif($_SESSION['F'.$j.'Tests'] == "Vision") {
                $prem = "60.00";
                $total += $prem;
                echo "Vision Test - $".$prem." per month";
            }
            elseif($_SESSION['F'.$j.'Tests'] == "BVCombo") {
                $prem = "122.95";
                $total += $prem;
                echo "Blood and Vision - $".$prem." per month";
            }
        ?>
        </td>
EDIT: I misunderstood the question, and see that you want the total for all enrollees. The code has been changed to do that.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论