applebiz89 Posted November 5, 2010 Share Posted November 5, 2010 I am trying to write a shopping cart script, that once they have confirmed their order it will insert into the database their order information. Basically as a product can have a number of different quanitities that they wish to purchase, I want to create an associative array that has a product, linked to the quantity. I want to do this so that the order information stays in one row in the database, so in the 'product' row, it has values like (cow -> 2) cow being the product and 2 being the quantity. Here is the code I am developing, and I have commented in the array which I am trying to create just the show my idea of thinking. any help, will be greatly appreciated! function updateCart($product_id,$id){ if($_SESSION['cart']) { foreach($_SESSION['cart'] as $product_id => $quantity) { $total = 0; $sql = sprintf("SELECT title, price, shipping FROM product WHERE pkID = %d;",$product_id); $result = query($sql); if(numRows($result) > 0 ){ $row = getResult($result); $price = $row['price']; $shipping = $row['ship']; $line_cost = ($price + $shipping) * $quantity; $total = $total + $line_cost; $product[] = $row['title'] -> $quantity; //unsure of how to code $sql2 = "INSERT INTO cart (userID, products, total, date) VALUES ('$id', '$product', '$total' '$date')"; $query = query($sql2); } } }else{ $alert = 'No items in shopping cart.'; } return $total; } Quote Link to comment https://forums.phpfreaks.com/topic/217864-how-to-create-an-associative-array-within-a-loop/ Share on other sites More sharing options...
jcbones Posted November 5, 2010 Share Posted November 5, 2010 I would put the items in the session as a multi-dem array, then you just add them to the cart from there. No need to hit the items table. //set the session. $_SESSION['items'][$item][$price] = $quantity; //now just send it to the database; foreach($_SESSION['items'] as $product => $v) { foreach($v as $price => $quantity) { setlocale(LC_MONETARY,'en_US'); $total = money_format("%i",$price * $quantity); $sql = "INSERT INTO cart (userID,products,total,date) VALUES ('$id','$product','$total','$date')"; } Code is for demo purposes only, it is incomplete, and un-tested. Quote Link to comment https://forums.phpfreaks.com/topic/217864-how-to-create-an-associative-array-within-a-loop/#findComment-1130768 Share on other sites More sharing options...
applebiz89 Posted November 5, 2010 Author Share Posted November 5, 2010 im sorry, I don't understand your example. Could you explain more please Quote Link to comment https://forums.phpfreaks.com/topic/217864-how-to-create-an-associative-array-within-a-loop/#findComment-1130821 Share on other sites More sharing options...
Anti-Moronic Posted November 5, 2010 Share Posted November 5, 2010 For such a complicated application that manages important information related to purchases, this is completely unsatisfactory. You should have separate tables to manage this data and use join queries and construct single sql queries to insert based on loops to parse user input. Your current method is hardly scalable and will be a pain to maintain as you develop and add to it. From what I can tell, you want to store one column as $product[] = $row['titile'] -> $quantity. That is as simple as storing an array in the column with the relevant values but it isn't reliable. You're dealing with inventory and you have to account for that. Say you need to check the order to ensure you have enough inventory, what do you do if all your quanties are stored within an array (but string) in a single column? You'd have to do a LIKE query or something. Bad idea. You need to download an existing shopping cart script which has been professionally developed and look at how you can better that, rather than trying to reinvent the wheel. Quote Link to comment https://forums.phpfreaks.com/topic/217864-how-to-create-an-associative-array-within-a-loop/#findComment-1130845 Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 OScommerce and Zen Cart are two pretty flexible and well written (from my experience) shopping cart applications. Both are open source and are free to use. Quote Link to comment https://forums.phpfreaks.com/topic/217864-how-to-create-an-associative-array-within-a-loop/#findComment-1130861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.