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.
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: