vendredi 29 janvier 2016

looping through result array and adding var from if elseif

I have a query that sums a column as totalSales grouped by id. I then take that result and run a if elseif statement on it.

 /* execute query */
$result->execute();
/* bind result variables */
$result->bind_result($accountNumber, $accountName, $totalSales);

 //get amount needed for next tier
//create variables, one from query and empty to hold result
$needed = "";

while ($result -> fetch()) {

if     ($row['totalSales'] < 1001.00) {
        $needed = (1001.00 - $row['totalSales']);
}
elseif ($row['totalSales'] < 2501.00) {
        $needed = (2501.00 - $row['totalSales']);
}
elseif ($row['totalSales'] < 5001.00) {
        $needed = (5001.00 - $row['totalSales']);
}
elseif ($row['totalSales'] < 10001.00) {
        $needed = (10001.00 - $row['totalSales']);
}
elseif ($row['totalSales'] < 15001.00) {
        $needed = (15001.00 - $row['totalSales']);
}
};

Then I take the results and add them to an array again.

$customers[] = array(
    'AccountNumber' => $accountNumber,
    'AccountName' => $accountName,
    'Amount' => $totalSales,
    'Needed' => $needed
    );

I get only one result instead of 5 grouped together with the amount needed to get to the next tier.

I have tried to explode the array and do it separately then add a the result back in using array_push but didn't add it to the array with the rest of the associative array. Can this be done in the while loop or do I need to another way.

Aucun commentaire:

Enregistrer un commentaire