Freedom-n-Democrazy Posted October 5, 2011 Share Posted October 5, 2011 I'm making a simple shopping cart. I have a strange issue when I add a product to the cart - the correct id registers ("1"), but the size registers as "0" when it should be "sizel". Can anyone see why is this happening? <?php if (isset ($_GET['action']) && $_GET ['action'] == add || size) { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = intval($_GET['size']); $_SESSION['cart'][] = $size; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } foreach ($_SESSION['cart'] as $size) { echo $size; } } ?> <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/ Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 $size = $_GET['size']; This fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275922 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Cancel that, it did not fix it. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275923 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 if (isset ($_GET['action']) && $_GET ['action'] == add || size) { Should be throwing undefined constant errors. If the size a required option? // If size is not required // if (isset($_GET['action']) && $_GET ['action'] == 'add') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = (isset($_GET['size']) ? $_GET['size'] : null); $_SESSION['cart'][] = $size; } // if size is required // if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275928 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 That fixed it. Thanks for that man it was really driving me up the wall. I'm new to this level of PHP so theres a bit I don't understand, but I'll get the hang of it soon. I've got one other small issue related related to the same code, I don't want to open another thread so I'll just ask here. I've got: } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } foreach ($_SESSION['cart'] as $size) { echo $size; } This produces double results and I can now see why, but is there a way to do something like this? elseif ($cart) { foreach ($_SESSION['cart']['id'] as $id) { echo $id; } foreach ($_SESSION['cart']['size'] as $size) { echo $size; } } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275935 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 When you are adding the elements to the array just add a key $_SESSION['cart']['id'] = $id; This method will only work if you are only adding one product to the cart. If you want to add more than one element to the cart, you should use an array (i wont write the code for you because its better to learn if you try it yourself) but basically. Your cart will store an array of products, each product element could also be an array containing the id, size and quantity values Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275938 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 i wont write the code for you because its better to learn if you try it yourself No, that style of learning is not for my personality. I get too frustrated with trying and failing over a million times. This cannot change, its who I am. I am not a Help Vampire. I am a visual learner. I learn best by looking at code and putting together its logic in my head. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275951 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 I tried your code: $_SESSION['cart']['id'] = $id ... but it did not work. elseif ($cart) { foreach ($_SESSION['cart']['id'] = $id;) { echo $id; } I don't see how it would work anyway, as there is a semi-column after $id I tried this also, but it failed: elseif ($cart) { foreach ($_SESSION['cart']['id'] as $id) { echo $id; } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275953 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 You cannot use foreach on a string, $_SESSION['cart']['id'] is the value (hence me saying you will only be able to add one product) Anyways, ill write something, you can merge it $_SESSION['cart']['products'][] = array( 'size' => $size, 'id' => $id, 'qty' => $qty ); Then when fetching the data you can use echo '<h3>Products</h3>' foreach ($_SESSION['products'] as $product) { echo 'Product ID: ' , $product['id'] , '<br/>Size: ', $product['size'] , '<br/>Quantity: ', $product['qty']; echo '<hr/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275954 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks for that, but theres one thing in the code that does not make sense to me. I see $product is used many times, but $product is not defined? EDIT: Sorry man, it is. Missed that! I'll try the code now and report back. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275955 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Sorry for a 10 minute reply. I don't copy and paste code that people show me, I write it up myself because it helps me better understand what is going on from another mental dimension. Ok, so I implimented the code style you suggested into what I had. It differs slightly form your code in terms of naming, but the code logic is the same. The code failed. All I get is "No product in cart.": <?php if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; } $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); if (!$cart) { echo "No product in cart."; } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275956 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 You really need to turn on your error_reporting It will show you whats going on. $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); Should be in your first IF statement and } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } should be } else {foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } } The other thing is I dont see anywhere in that code where $cart is defined. Try using if (empty($_SESSION['cart']['content'])) { // throw no products error } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275958 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 if (!$cart) { echo "No product in cart."; The $cart variable is not defined anywhere, also, each time you use $_SESSION['cart'][] = ??? a new array is created in the $_SESSION['cart'] variable. Have you ever used a ternary operator? session_start(); if ( !isset($_SESSION['cart']) ) $_SESSION['cart'] = array(); if ( isset($_GET['action']) && !strcasecmp($_GET['action'], 'add') ) { $cart = array(); $cart['id'] = isset($_GET['id']) ? (int)$_GET['id'] : ''; $cart['size'] = isset($_GET['size']) ? $_GET['size'] : ''; $_SESSION['cart'][] = $cart; } if ( empty($_SESSION['cart']) ) { echo 'No products in cart.'; } else { foreach($_SESSION['cart'] AS $item) : echo '<pre>' . print_r($item, 1) . '</pre>'; endforeach; } Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275959 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Buddski, I followed your explanation but the page totally failed and error said "PHP Parse error: syntax error, unexpected T_VARIABLE, expecting '(' on line 38", which is: if $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); <?php if $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); else {foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } if (empty($_SESSION['cart']['content'])) {echo "No product in cart";} ?> From my understanding of PHP, this makes no sense at all. :'( I'm just going round and round and round in circles. Could you please write it from start to finish so I can see clearly how its engineered? The last explination totally blew my mind and I've totally lost it. If I can see the complete code I can sit here and see how everything works and how its done. Each code example just fucks my mind. I got ADD. I can't all this. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275962 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Andy-H, I'm continuing on with the current code structure, because starting over again just means more head aches. But thanks any man, really!!!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275965 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 I have reverted back to the last state where the page loaded. I still cannot add a product into the session. The code sits at this: <?php if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; } $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); if (!$cart) { echo "No product in cart."; } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } ?> A product is added with this: <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275967 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Awesome. Blow my mind and then desert me. Good brotherhood here. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275971 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 My point why I learn from visual learning. This is why I ask to see code - so I can go off and teach myself. Most people cannot explain very well. http://i53.tinypic.com/2vcyws6.png Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275972 Share on other sites More sharing options...
MasterACE14 Posted October 5, 2011 Share Posted October 5, 2011 one typo in Buddski's code: this line: if $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); is missing the first parenthesis(hence your parse error): if($_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275974 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Yeah well, thats still not help for me, I've been told that code should go in 2 different places. Now I don't know what to do. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275976 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 I just tried this code also, but total failure! <?php if ($_SESSION['cart']['content'][] = array) {'size' => $size, 'id' => $id}; $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; if (empty($_SESSION['$cart']['content'])) { echo "No product in cart."; } else {foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } ?> PHP Parse error: syntax error, unexpected ')', expecting '(' on line 38:/QUOTE] if ($_SESSION['cart']['content'][] = array) {'size' => $size, 'id' => $id}; Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1275982 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 <?php if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; } $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); if (!$cart) { echo "No product in cart."; } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } ?> It will show you whats going on. $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); Should be in your first IF statement and } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } should be } else {foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } } The other thing is I dont see anywhere in that code where $cart is defined. Try using if (empty($_SESSION['cart']['content'])) { // throw no products error } I have done this and still failure. Perhaps if you can write the code out in full, then there will be no error in quote interpretation and I will be able to understand what is going on with the PHP logic. This is what I did on interpretation of your instructions. <?php if ($_SESSION['cart]['content'[] = array ( 'size' => $size, 'id' => $id); $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = $_GET['size']; $_SESSION['cart'][] = $size; } if (empty($_SESSION['cart]['content'])) { echo "No product in cart."; } else foreach ($_SESSION['content'] as $content) { echo $content['id'] , $content['size']; } ?> I think you can agree with me now why I am a visual lerner. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1276006 Share on other sites More sharing options...
KevinM1 Posted October 5, 2011 Share Posted October 5, 2011 Part of the problem is that you're panicking and throwing shit at the wall to see if it will work, without actually understanding what your code is doing. This is evidenced by you misunderstanding what Buddski was trying to tell you. So, take a deep breath and calm down. Programming is frustrating, but getting worked up over it will only make it harder for you to learn. Also, comments like: Awesome. Blow my mind and then desert me. Good brotherhood here. Will only lead to people not wanting to help you. Everyone here - including those of us with badges under our names - are volunteers. We don't see one red cent for being here. No one is obligated to help you. Remember that when you get frustrated. So, all that said, here is what Buddski was trying to tell you: if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $_SESSION['cart']['content'][] = array ( 'size' => $size, 'id' => $id); // <-- add item to your cart's content array } if (empty($_SESSION['cart']['content'])) // <-- if the cart doesn't contain any content { echo "No products in cart"; } else { foreach ($_SESSION['cart']['content'] as $content) { echo "{$content['id']}, {$content['size']}"; } } Finally, you would be well served to brush up on the basics. Basic if/else syntax, arrays, echo syntax... a lot of the most basic stuff is tripping you up, making it a lot harder for you to get results. You're trying to run without being able to even crawl. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1276011 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 This is evidenced by you misunderstanding what Buddski was trying to tell you. I couldn't understand it because it was written up poorly. Also, comments like: Awesome. Blow my mind and then desert me. Good brotherhood here. Will only lead to people not wanting to help you. Everyone here - including those of us with badges under our names - are volunteers. We don't see one red cent for being here. No one is obligated to help you. Remember that when you get frustrated. I was just saying the truth man. He deserted me. I know everyone here are volunteers and they don't have to help me, but if they are going to help and I point out I suck at learning academically and that I am visual learner, then respect this and help me in a visual way or don't help me at all, not continue on helping me in a way that I said was confusing from early conversation and then desert me.. and that is why I made that comment. Finally, you would be well served to brush up on the basics. Basic if/else syntax, arrays, echo syntax. I am useally good at them, but like you said, I panicked and started throwing shit at a wall to see what would happen. I followed the code you suggested and the page has returned to working again. I have things adding to the session, but here is the strange thing.. they are added as "," (single comma), not the 'id' and 'size' names. Here is the PHP code: <?php session_start(); if (isset($_GET['action']) && isset($_GET['size']) && $_GET ['action'] == 'add') { $_SESSION['cart']['content'][] = array ( 'side' => $size, 'id' => $id); } if (empty($_SESSION['cart']['content'])) { echo "No product in cart."; } else { foreach ($_SESSION['cart']['content'] as $content) { echo "{$content['id']}, {$content['size']}"; } } ?> ... and here is the HTML code: <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Really strange, I can't see any commas anywhere in the HTML code, its also stranger that only 1 comma is showing. ... and thanks for helping me understand how the logic flow works. now that I have chilled out and read it its really quite clear now. Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1276017 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 Firstly, I didnt "desert you", I am in no way assigned to your specific problem, this is a FORUM used for discussing problems, if somebody can take what I have advised you and in some way improve on that then that is fantastic for you. Everybody has a different approach to problems. I post what I think will help you, being a visual learner. I am aware that I need to work on my instructional delivery (comments and the like) but saying that I deserted you is just rude to be honest. But whatever, in regards to your code are you referring to the echoing of the elements displaying with a , (comma)? echo "{$content['id']}[color=red][b],[/b][/color] {$content['size']}"; That is where your comma is hiding. That echo statement will echo the ID from the session and then the size separated by a comma. So your output from the below example will be 1, sizel Quote Link to comment https://forums.phpfreaks.com/topic/248463-strange-outcoming-using-action-in-a-hyperlink/#findComment-1276024 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.