wmguk Posted March 20, 2009 Share Posted March 20, 2009 Hey, I have a shopping cart, and the script inserts the product id in to a session called cart... it makes them an array too... for example if i order 1 x PROD1 and 3 x PROD4 the session shows PROD1,PROD4,PROD4,PROD4 and what i need to do is enter that info into an order table, and show it on the page.... I'm using: if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } foreach ($contents as $id=>$qty) { $productsql = "SELECT * FROM products WHERE code = '$id'"; $productresult = mysql_query($productsql); while($prod = mysql_fetch_array($productresult)) { $pcode = $prod['code']; $pname = $prod['name']; $pprice = $prod['price']; } $ins = "INSERT INTO orders (order_id, name, add1, add2, town, county, postcode, prodname, prodcode, prodprice) VALUES ('$ordid', '$name', '$add1', '$add2', '$town', '$county', '$postcode', '$pname', '$pcode', '$pprice')"; mysql_query($ins); } } but what that does for the order of 4 items above is create 2 rows in the DB, one with PROD1 detail, and the other with PROD4 details, but also doesnt say the quantity of each item... I dont know the best way to hold the order, as I need to know QTY, ITEMID, PRICE, NAME, but also a subtotal for the order and after they add delivery a total... I thought about have two tables, one for the order details, and one for the overall order.. ie, ORDERREF | QTY | ITEMID | NAME | PRICE and then | ORDERREF | SUBTOTAL | DEL | TOTAL but i dont know how to read the array of products in the session, count the different items to work out there are 3 of PROD4 etc.... any ideas? Link to comment https://forums.phpfreaks.com/topic/150278-reading-a-session-header-array/ Share on other sites More sharing options...
WolfRage Posted March 20, 2009 Share Posted March 20, 2009 Well you could sort that product array into a more conducive array, by searching for a new product name if name of product in the more conducive array does not exist, add it as a new product and quantity gets ++ or 1. Then continue this; if product name matches product in our more conducive array then quantity gets ++ but no new product name is added. Does that make sense and can you code it? Link to comment https://forums.phpfreaks.com/topic/150278-reading-a-session-header-array/#findComment-789628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.