thomashw Posted January 10, 2008 Share Posted January 10, 2008 For the following code, when the link is clicked, what will the session be storing exactly? I'm trying to do this on multiple pages, with different product_id's, so when clicked they will be held in a session and then I can display them in a shopping cart. Will this work? <? session_start(); if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } if (isset($_GET['buy'])) { $_SESSION['cart'][] = $_GET['product_id']; } ?> <form method="post" name="itemselect" action="<? echo "$_SERVER[php_SELF]?product_id={$id}" ?>" target="purchaseform"> Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/ Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 well. If 'cart' exists in the session array, then $id is added to the end of the cart array Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435886 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 product_id=$id isn't added? What I want to do is add multiple product_id's to a "cart" and then have the user be able to view the products they've added (all the products and their id's are in my database.) I have the session counting how many products are in the cart, and it's working, I'm just not sure how to "extract" the product_id information from the session. Could someone explain? Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435888 Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 no. because your query sting is ?product_id=X&buy=X you are doing $_SESSION['cart'][] = $_GET['buy']; Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435894 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 I've removed the "buy" and changed it to $_GET['product_id']. How can I extract the multiple product_id's from the session to call them from my database? Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435898 Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 Well $_SESSION['cart'] is an array. You can loop though it or access by specific key. for example foreach($_SESSION['cart] as $key => $val){ echo 'key: ' . $key .' val:' . $val . '<br />'; } I dont know what type of query you are trying to run, but that should be how you access the info Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435903 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 <? echo ($_SESSION['cart'][1]); ?> Would that work as well? Maybe have the [1] in a while loop so it shows all the product_id values? Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435909 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 My goal is to store all of the product_id values the user submits (from the form) in the session, and then to call these product_id values from my database. How should I go about doing that? Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435911 Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 since the product_id is an array, you can easily make it a string to use for your db query $product_ids = implode(',', $_SESSION['cart']); and your query would look like this "SELECT * FROM table WHERE product_id IN (". $product_ids .")"; Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435920 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 I've never used IN before in a select query, can you explain what it does? I tried searching but couldn't find any information on the IN part. Sorry for asking so many questions. Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435927 Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 in is kinda like php's in_array so your query would look like this "SELECT * FROM table WHERE product_id IN (1,2,3)"; Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435944 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 Ahh, okay. Could I keep the rest of my SELECT statement, or would I need to change it (I need each product_id to apply to the statement.) $query = mysql_query("SELECT * FROM feature_product, feature_group, feature_item WHERE feature_product.feature_product_id IN (". $product_ids .") AND feature_group.feature_product_id=feature_product.feature_product_id AND feature_item.feature_group_id=feature_group.feature_group_id"); Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435951 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 Nevermind, figured that one out. BUT, now no matter which product I add (product_id=2&product_buy=2, for example) it just shows the first product again (product_id=1&product_buy=1). I think this has to do with the implode, but I don't know how it works. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435974 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 Seems like this is more of a blog of mine now. I did it using a while loop - I guess I should wait a bit longer before asking questions. Thanks for all the help emehrkay!! Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-435991 Share on other sites More sharing options...
revraz Posted January 10, 2008 Share Posted January 10, 2008 At least you are trying and figuring it out for yourself Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-436007 Share on other sites More sharing options...
thomashw Posted January 10, 2008 Author Share Posted January 10, 2008 At least you are trying and figuring it out for yourself Sometimes I'll try to figure something out for hours and then ask, and about 5 minutes after I ask I'll figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/85432-solved-sessions/#findComment-436008 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.