tags:

views:

52

answers:

2

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

+2  A: 

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.

Artefacto
already done that - it doesnt work. Only returns the last $prem value in the array since coming within a FOR loop already
JM4
@JM4 Did you include the `[]`? It basically does a push operation on the array.
Artefacto
@Artefacto - got it, I didn't include the array variable outside of the loop to begin. Thanks!
JM4
@JM4 It's not strictly necessary; if it's not defined the first `$var[] = ...` will initialize the array; nevertheless it's good practice because sometimes the loop that follows may have no iterations.
Artefacto
@Artefacto - without defining before it does cause errors. Also, the echo statements don't function 'quite' as they should. When changing $prem to $prem[], the echo "Blood test - $".$prem." per month" will throw an error.
JM4
to clarify - leaving the definition pre-loop is ok, only if changing the name of the variable altogether. Using "$prem" causes errors when echoing. Thank you for your help!
JM4
+1  A: 

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.

GSto
@GSto - I am already able to get the total for a single enrollee by simply looking at $prem, I am trying to get the totals for ALL enrollees. An individual enrollee can only have 1 $prem variable based on the IF statements. I think you have something with the += though
JM4
@JM4 : i updated the code so it gives you the total of all enrollees.
GSto