123guy Posted January 7, 2013 Share Posted January 7, 2013 (edited) Hi all. Question for you. I am setting up a shopping cart and I would like the main page to have divs set aside to showcase products and allow for a quick add to cart. I only want the customer to be able to quick add one so that they don't go over the max limit on accident. on the checkout page, I have it where they can change the quantity, but I have form validation set up so that it can't go over max quantity. So my question is, how do I run a loop that selects the products, then another query finds out whether that product is in the cart already or not. there is a max of four products on the home screen. this is what I have so far. <?php $result = mysql_query("SELECT * FROM items order by quantity ASC LIMIT 4"); $i = 0; echo " <table cellspacing='20px' border='10' frame='void' ><tr>"; while($row = mysql_fetch_array($result)) { $title = $row['name']; $id = $row['id']; $price = $row['price']; $image = $row['image']; $qty = $row['quantity']; echo "<td valign='top' style='border-radius:20px' width='150'bgcolor='#CBE8F9'>"; ?> </p> <a href="product.php?id=<?php echo $id; ?>"> <center><img src="<?php echo $image; ?>" width="140" /></center><br> <?php echo $title; ?><br /> <font color="red"><?php setlocale(LC_MONETARY, "en_US"); echo money_format("%n", $price);?></font></a><br /> <form action="addtocart.php" method="post"><input name="product" type="hidden" value="<?php echo $title; ?>" /><input name="price" type="hidden" value="<?php echo $price; ?>" /><input name="id" type="hidden" value="<?php echo $id; ?>" /><input name="qty" type="hidden" value="1" /><input type="hidden" name="image" value="<?php echo $image; ?>" /><?php $result1 = mysql_query("SELECT * FROM cart where cart='".session_id()."' AND product_id='".$id."' AND qty !='0'"); while($row1 = mysql_fetch_array($result1)) { $qty=$row1['qty']; if(!$result){ ?><input name="submit" type="submit" value="Add To Cart" /><?php } if($result){echo 'Item is already in cart, you may change the quantity there';}} ?></form></div> <?php echo "</td>"; if ($i && $i%6 == 5) echo '</tr><tr>'; $i++; } echo "</table> "; ?> but obviously it's not working, that's why I am here. It currently says all products are in cart and it also runs the query for the 2nd product 2 times. any help on this? Edited January 7, 2013 by 123guy Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/ Share on other sites More sharing options...
123guy Posted January 8, 2013 Author Share Posted January 8, 2013 what is the best way to do this? I have read about joins, but I am not really sure how they work or if this is the best situation for one. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404208 Share on other sites More sharing options...
cpd Posted January 8, 2013 Share Posted January 8, 2013 I'm not entirely sure what you want as you've asked a few different questions. I think you should explain clearly what your goal is in the simplest way possible. Your current explanation isn't very coherent which may explain the lack of responses. Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404225 Share on other sites More sharing options...
scootstah Posted January 8, 2013 Share Posted January 8, 2013 How are you storing items in the cart? Does that go to a database table, to a session, to a cookie, etc? In any case, I would say you should first select that data separately. Then, run your query to select all products. Then when you loop through all products, see if the ID is amongst that of the "added" IDs. Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404236 Share on other sites More sharing options...
123guy Posted January 8, 2013 Author Share Posted January 8, 2013 ok, so the goal is to have one query run that pulls products out of my database and duplicates divs to display these in....this works fine. I then need to run another query for each of the products that were just pulled and see if the current session has the products in the cart. The cart is stored in a database. If the cart already has that product, I would like a message to display, "go to cart to change quantity"....if it is not present in the cart, then it shows an add to cart button. does this make a little more sense? Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404246 Share on other sites More sharing options...
cyberRobot Posted January 8, 2013 Share Posted January 8, 2013 You should be able to grab the products table and use a Left Join (http://www.tizag.com...sqlleftjoin.php) to get the user's order information based on the product ID. You'll need to grab something from the user order database, like the row ID. Then as you're looping through the products, you can check if the row ID field contains a value. If it does, display the warning. Otherwise, display the button. As a side note, I would recommend against passing product information (such as price) through the form. <form action="addtocart.php" method="post"><input name="product" type="hidden" value="<?php echo $title; ?>" /><input name="price" type="hidden" value="<?php echo $price; ?>" /> It's fairly easy for someone to re-create the form, modify the price to 0, and submit the results to your addtocart.php script. Instead, you can pass just the product ID. The addtocart.php script would then use the ID to get the necessary product information. Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404253 Share on other sites More sharing options...
Psycho Posted January 8, 2013 Share Posted January 8, 2013 You can do this with a simple query - no need to select all the record and loop over them. For the products that are already in their cart you would obviously be storing this information somewhere AND that information should be based off the product ID. You just need to create a select query that uses the NOT IN condition with the list of product ids that are in the user's cart. I will provide two examples based upon how you might be storing the cart info. Cart info stored in a 'cart_prod' table. This assumes there is a carts table where the cart is associated with the user and the cart_prod table is for the products associated with each cart entry. SELECT prod_id, prod_name FROM products WHERE prod_id NOT IN (SELECT prod_id FROM cart_prod JOIN cart ON cart.id = cart_prod.cart_id WHERE cart.user_id = '$user' ) Or, if the cart is stored in a session variable. This assumes the cart is stored as a multi-dimensional array in the format $_SESSION['cart'][{PROD_ID}] = {QUANTITY} $cart_ids = impldode(',', $_SESSION['cart']); $query = "SELECT prod_id, prod_name FROM products WHERE prod_id NOT IN ($cart_ids)"; Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404268 Share on other sites More sharing options...
123guy Posted January 8, 2013 Author Share Posted January 8, 2013 ok, thanks, how do I display the message though? I would still like all the products to display, but I want the button to change to my message. My table structures are items: id, product, price, etc cart: id, product_id, price, etc. having never worked with joins, I am not sure what I should change to fit what....in the query. could you help me with these? Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404309 Share on other sites More sharing options...
cyberRobot Posted January 8, 2013 Share Posted January 8, 2013 having never worked with joins, I am not sure what I should change to fit what....in the query. could you help me with these? Did you see the link I posted in Reply 6? It may help you with the join. Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404313 Share on other sites More sharing options...
Psycho Posted January 9, 2013 Share Posted January 9, 2013 ok, thanks, how do I display the message though? I would still like all the products to display, but I want the button to change to my message. My table structures are items: id, product, price, etc cart: id, product_id, price, etc. having never worked with joins, I am not sure what I should change to fit what....in the query. could you help me with these? What are you talking about? What "message"? Your original post stated: So my question is, how do I run a loop that selects the products, then another query finds out whether that product is in the cart already or not. there is a max of four products on the home screen. I provided examples of queries that will select from all the products that are not already in the cart. If you don't want users to "quick add" products that are already in their cart then don't provide those products for Quick Add. Making those products available only to have to provide an error message if they actually select it is kinda stupid in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/272818-run-loops/#findComment-1404484 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.