Adding Database Results to an Array

Hello,

I’m trying to put together a discount calculation script to add to a shopping cart and I’m having a couple of problems.

The place I’m stuck at the moment is that I’m looping through the results of the query which is giving me the ID’s of all products included in the offer that exist in the cart’s SESSION. If more than one of the same item has been bought I was hoping make extra array elements to reflect this, which will allow me to do the necessary calculations later on. Here’s some code, anyone see what I’m doing wrong?


    $prod_discounts = array();
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        
         for ($i = 1; $i <= $_SESSION['cart'][$row['prod_id']]['quantity']; $i++) {
            
             $prod_discounts[$row['prod_id']] = $_SESSION['cart'][$row['prod_id']]['price'];
           
        }
        
    }

Cheers,

Jon

Thanks for identifying the problem, I’ve now got it working and here’s the code, if anyone else has a similar situation.



$buy_quantity = 6;

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        
        for ($i = 1; $i <= $_SESSION['cart'][$row['prod_id']]['quantity']; $i++) {
            $prod_discounts[] = array( 'id' => $row['prod_id'], 'price' => $_SESSION['cart'][$row['prod_id']]['price']);
           
        }
        
    }

function calc_discount($prod_discounts, $buy_quantity) {
    
    $free_items = 0;
    
    if (count($prod_discounts) > $buy_quantity - 1) {
        $free_items = count($prod_discounts) / $buy_quantity;
        $free_items = floor($free_items);
    }
    
    function discount_sort($x, $y) {
        return($x['price'] > $y['price']);
    }
    
    uasort($prod_discounts, 'discount_sort');
    
    $prod_discounts = array_slice($prod_discounts, 0, $free_items);
    
    $discount = 0;
    
    foreach ($prod_discounts as $v1) {
        foreach($v1 as $k2 => $v2) {
            if ($k2 == 'price') {
                $discount += $v2;
            }
        }
    }
    
    return $discount;
    
}

$discount = calc_discount($prod_discounts, $buy_quantity);


Cheers,

Jon

The problem you’re running into is that you cant have multiple array items with the same index (In this case, the product ID).

Could do something like this… (I end up with a value $discount that reflects the correct discount. Feel free to replace $discount with whatever you need to assign the discount to)


function myarraysort($x, $y) {
 if ($x['price'] == $y['price'])
   return 0;
 return ($x['price'] < $y['price']) ? -1 : 1;
}
$discount = 0;
uasort($_SESSION['cart'],'myarraysort');
if (array_reduce($_SESSION['cart'], create_function('$total, $next', '$total += $next["quantity"]; return $total;')) > 6) {
  $topitem = array_shift($_SESSION['cart']);
  $discount = $topitem['price'];
  array_unshift($_SESSION['cart'],$topitem);
}

The offer is that if you buy 6 items, one of those is free so I was hoping to use the price value in the array to calculate the cheapest item(s) price and deduct it from the total in the shopping cart.

So I’d have thought that a for loop would have been the way to go, unfortunately it won’t add the extra elements to the array but if I echo $i within the loop it displays numbers in the way I’d expect. I’m stumped.

Cheers,

Jon

Inside your while loop, you don’t need to add for statement to add to an array those items that has more than one quantity but instead you could just add an if statement checking if the quantity is more than one and add it to the $prod_discounts array. your code could look something like this:


    $prod_discounts = array();
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        
		if($_SESSION['cart'][$row['prod_id']]['quantity'] > 1) {
			
			$prod_discounts[$row['prod_id']]['price'] = $_SESSION['cart'][$row['prod_id']]['price'];
			
			$prod_discounts[$row['prod_id']]['extra_items'] = $_SESSION['cart'][$row['prod_id']]['quantity'] - 1;
			
		}
        
    } 

Hope it can help :slight_smile: