How to handle cart recordset into group?

Discussion in 'PHP' started by cathaylist.com, Oct 2, 2009.

  1. #1
    One of my thoughts need to split cart into brand group, I am not sure how to make it.

    MySQL
    Cart recodset:
    ----------------------------------------------------
    Cart_id | brand | product | amount
    ---------------------------------------------------
    1 | nokia | 6500c | 999999
    8 | samsung | L708 | 555555
    9 | nokia | 6700 | 666666
    55 | samsung | J808 | 333333

    I am not sure how to use PHP to show the above data on web as

    Brand 1: nokia
    6500c | 999999
    6700 | 666666

    Brand 2: samsung
    L708 | 555555
    J808 | 333333

    I didn't find much examples via Google.

    Does anyone know how to make it or example? THX.
     
    cathaylist.com, Oct 2, 2009 IP
  2. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Assuming you have all cart items in an array, like this
    
    $items = array(
        array('Cart_id'=>1, 'brand'=>"nokia", 'product'=>"6500c", 'amount'=>999),
        array('Cart_id'=>8, 'brand'=>"samsung", 'product'=>"L708", 'amount'=>555),
        array('Cart_id'=>9, 'brand'=>"nokia", 'product'=>"J808", 'amount'=>666),
        array('Cart_id'=>55, 'brand'=>"samsung", 'product'=>"6500c", 'amount'=>333)
    );
    
    PHP:
    you could do foreach loop and sort items in another array, like this
    
    $itemsSorted = array();
    foreach($items as $val) {
        $itemSorted[$val['brand']][] = $val;
    }
    
    PHP:
    so it would output something like this
    
    $itemsSorted = array(
        'nokia'=> array(
            array('Cart_id'=>1, 'brand'=>"nokia", 'product'=>"6500c", 'amount'=>999),
            array('Cart_id'=>9, 'brand'=>"nokia", 'product'=>"J808", 'amount'=>666)
        ),
        'samsung'=> array(
            array('Cart_id'=>8, 'brand'=>"samsung", 'product'=>"L708", 'amount'=>555),
            array('Cart_id'=>55, 'brand'=>"samsung", 'product'=>"6500c", 'amount'=>333)
        )
    );
    
    PHP:
     
    Gray Fox, Oct 2, 2009 IP
  3. cathaylist.com

    cathaylist.com Well-Known Member

    Messages:
    437
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #3
    thank you very much, Gray Fox.
    It's exactly that I need.
     
    cathaylist.com, Oct 2, 2009 IP