SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    PHP Code:
        $prod_discounts = array();
        
        while(
    $row mysqli_fetch_array($resultMYSQLI_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

  2. #2
    SitePoint Member
    Join Date
    May 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
        $prod_discounts = array();
        
        while(
    $row mysqli_fetch_array($resultMYSQLI_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

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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)

    PHP Code:
    function myarraysort($x$y) {
     if (
    $x['price'] == $y['price'])
       return 
    0;
     return (
    $x['price'] < $y['price']) ? -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);


  5. #5
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for identifying the problem, I've now got it working and here's the code, if anyone else has a similar situation.

    PHP Code:

    $buy_quantity 
    6;

    while(
    $row mysqli_fetch_array($resultMYSQLI_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_discounts0$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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •