nepoverljiv Posted December 27, 2012 Share Posted December 27, 2012 Hi, I make some code and almost finish my shopping cart on website. Just need finally touch . Ok, I have problem on last page, problem is like this. When someone click to order what he putted in shopping cart, on one page I wanna list everything what he ordered an send on e-mail. Trick is I don't have only id in array, I have almost quantity. For example, if someone order article with id=1 and quantity 3, and article with id=2 and quantity 4, and article with id=3 and quantity 2. On my page I get all that in array like this: $array=$_POST['id']; When I print that I get this: 1-3,2-4,3-2 How to I get from my table "description", list of every article in this example and immediately multiply them price with quantity? I try something with: $id_quantity_pair = explode("-", $array); // Uses Hyphen(-) as delimiter to separate product ID from its quantity $product_id = $id_quantity_pair[0]; // Get the product ID $product_quantity = $id_quantity_pair[1]; But I dont know how to put that in list, I know only to working with array with one value. Anyone to help me? Quote Link to comment https://forums.phpfreaks.com/topic/272416-php-array-and-listing-description-from-table/ Share on other sites More sharing options...
NomadicJosh Posted December 27, 2012 Share Posted December 27, 2012 Have you tried foreach? Quote Link to comment https://forums.phpfreaks.com/topic/272416-php-array-and-listing-description-from-table/#findComment-1401569 Share on other sites More sharing options...
nepoverljiv Posted December 28, 2012 Author Share Posted December 28, 2012 I try like this: $array=$_POST['pid']; $id_str_array = explode(",", $array); // Uses Comma(,) as delimiter(break point) $fullAmount = 0; foreach ($id_str_array as $key => $value) { $id_quantity_pair = explode("-", $value); // Uses Hyphen(-) as delimiter to separate product ID from its quantity $product_id = $id_quantity_pair[0]; // Get the product ID $product_quantity = $id_quantity_pair[1]; // Get the quantity $sql = mysql_query("SELECT * FROM mytable WHERE id='$product_id' LIMIT 1"); while($row = mysql_fetch_array($sql)){ $product_price = $row["price"]; echo $product_price; //show all price } $product_price = $product_price * $product_quantity; $fullAmount = $fullAmount + $product_price; } Where I wrote show all price, I printing all price, but if I add also id to print, he will print me only id for every article or only price for each article depends what I first echo. Can anyone help me with this, because I am close to finish? Quote Link to comment https://forums.phpfreaks.com/topic/272416-php-array-and-listing-description-from-table/#findComment-1401830 Share on other sites More sharing options...
Christian F. Posted December 28, 2012 Share Posted December 28, 2012 I think you've made this quite a lot more complicated than what you needed, thus you've created a lot of extra problems for yourself. Biggest complication is the format you've decided to return the item IDs and quantity in, which means you'll have to add a lot of code just to parse the logic of it. It would have been much better to simply return that data in an array, which would allow you to either directly access the Items or loop through them. Without all of that extra code. Also, you should never run a DB query inside a loop: Nothing kills performance quite like that, and it's quite unnecessary as well. Especially in this case, where you could just fetch all the items with one query, using the IN() operator to match all IDs at once. I would recommend you to read through this article on creating a shopping cart, and then rethinking your approach to this. Keep it simple, and it'll be simple to work with. Though, to be able to keep it simple, you have to understand the problem and solution fully, before starting to write the code. Quote Link to comment https://forums.phpfreaks.com/topic/272416-php-array-and-listing-description-from-table/#findComment-1401865 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.