Drummin Posted May 27, 2011 Share Posted May 27, 2011 I think you might need to quote your true false values for $wasFound = "false"; as they are resulting in 1 or 0 instead of true or false. Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 also if you want to make sure your not goofing something up before you start adding code use an alert box to test the below will create a popup and show the id data if you named the button id pid_whatever this will read it make sure you specify the table name and you have declared jquery in your incudes  <script> $('#yourtablename input[id^=pid_]:not(.ui-pid_)').live("click",function(){ var idData = $(this).attr('id'); var id_data=idData.split('_'); $("#voucher_id").val(id_data[1]); alert(idData); }); </script> Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 no... no alert at all... That code makes the 1st item liste to display blank page as well. Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 28, 2011 Share Posted May 28, 2011 no... no alert at all... That code makes the 1st item liste to display blank page as well.  then its something in your code because this works for me, i copied it out of a working module and just change the id  make sure 1) jquery is declared for example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="jquery-1.4.2.min.js" type="text/javascript"></script>  <script> $('#yourtablename input[id^=pid_]:not(.ui-pid_)').live("click",function(){ var idData = $(this).attr('id'); var id_data=idData.split('_'); $("#voucher_id").val(id_data[1]); alert(idData); }); </script> </head> <body> you can find this stuff at http://jquery.com/  create some html for example a table with some buttons named pid_1, pid_2, pid_3 ect and test to make sure you get that right then look in your code to see where the oopsey is  this works for me i have used is quite a few times, maybe a hundred in various routines on my pages  sometimes its the very obvious, a slip up in typing ect  i use a debug line when im having trouble  for ex $debug_message="START DEBUG</br>"; $debug_message.=$whatever." = whatever</br>"; $debug_message.="END DEBUG</br>"; and after all is done the last line in my page is echo $debug_message; this has helped me find many a goof    Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 28, 2011 Share Posted May 28, 2011 I've been playing with the processing side of this for the past two days and adding a product and updating the quantity is not a problem for a single item_id. Even adding a second item is not a problem but from that point on everything stops because you no longer have unique $key values, i.e. item_id etc. The idea of making this work sounds cool but I think you'll need to resort to putting the values in a mysql table where you would also have a record of their order for future reference. That would be straight forward and easy and I'm not sure why you're taking the session approach. In any case, hope you get it worked out. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 28, 2011 Author Share Posted May 28, 2011 this is how I call jquery: <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> and because I am doing all my scripts external, thats how I call each script: <script type="text/javascript" src="help_scripts/eachscript.js"></script> Would it be better to have the script inside my code? Â Drummin... to make the items and form getting unique id I think is easy... isn't it???? I mean by adding the $id variable to their ids... Or am I wrong?? Am I missing anything?? The thing is to find out where the code stuck and make it happen And because I am no expert my self.... I need your help Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 28, 2011 Author Share Posted May 28, 2011 if we give unique ids like this: <form name="bd_itm'.$id.'" id="bd_itm'.$id.'" method="post" action="help_scripts/cart_functions.php"> <input type="hidden" name="pid'.$id.'" id="pid'.$id.'" value="' . $id . '" /> <input type="submit" name="button'.$id.'" id="button'.$id.'" value="Add to Cart" /> how should this change:(?) if (isset($_POST['pid'])) { Â Â $pid = $_POST['pid']; or it will not work? Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 28, 2011 Share Posted May 28, 2011 Changing the form side of things using variable names will bring it's own problems as you pointed out. You'll need to use a generic $_POST for processing and extract the key and values while also filtering out the submit button itself much like I posted the other day. Something like this. foreach($_POST as $key => $val){ IF ($key!="button"){ //DO SOMETHING WITH THIS $key ID and $Value// } } You'll then need to check if you have the $key item set to session and if you do, add +1 to the quantity. IF $key is not found in session add the item to session. This approach might work. The value saved for item_id# doesn't matter, just be constant like "in-cart" in the array you'd have the unique key in your array something like this, array([item_id#] => in_cart). So you'll be comparing foreach($_post as $key) to that item_id#.   Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 29, 2011 Author Share Posted May 29, 2011 So I have to change this: if (isset($_POST['pid'])) {   $pid = $_POST['pid']; $wasFound = "false"; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {   // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) {    $i++;    while (list($key, $value) = each($each_item)) {  if ($key == "item_id" && $value == $pid) {  // That item is in cart already so let's adjust its quantity using array_splice()  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));  $wasFound = true;  } // close if condition    } // close while loop    } // close foreach loop  if ($wasFound == false) {  array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));  } }   exit(); } to something like: foreach($_POST as $key => $val){ IF ($key!="button"){ $wasFound = "false"; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {   // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $val, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) {    $i++;    while (list($key, $value) = each($each_item)) {  if ($key == "item_id" && $value == $val) {  // That item is in cart already so let's adjust its quantity using array_splice()  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $val, "quantity" => $each_item['quantity'] + 1)));  $wasFound = true;  } // close if condition    } // close while loop    } // close foreach loop  if ($wasFound == false) {  array_push($_SESSION["cart_array"], array("item_id" => $val, "quantity" => 1));  } }   exit(); } } Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 29, 2011 Share Posted May 29, 2011 The problem is the KEYS in the array need to be unique so you can't use item_id, quantity as in [item_id] => pid [quantity] => 1. I would suggest just using the product id as the KEY and the quantity as the value. [pid] => 1. Then you should be able to find this unique KEY[pid] and udate the VALUE for this item. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 30, 2011 Author Share Posted May 30, 2011 Hello, Â I am playing around with it but it seems something I am missing. Â I will ask a silly (maybe) question... Â 1. If the add-to-cart problem is that I don't have unique ids to the items created by the dynamicList, then what ids do I have???? Because if I had let's say the same id as item1 listed, when clicking the add-to-cart button it should add (at least) the same item to the cart. Or not???? Â 2. Using the $id I am getting the picture of my each item from db, that means that the $id is unique for each item in my dynamicList. Â 3. Given the points 1 & 2 could it be something else that doesn't let each form to be proccessed and add the items to the cart? Â Thank you. Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 30, 2011 Share Posted May 30, 2011 It's in the processing array, which you are saving to session. Where you are saving "item_id => $pid", the "item_id" is the "key" and "$pid" is the "value". This won't work because the "key" needs to be unique.  I've made more than a dozen variations of the processing script but kept running into problems until I created a new session for each item. I then needed to create another session just for tracking the cart_keys and have got that working as well. Now I just need to create a usable display so you can see what is being saved, because it might be hard to understand otherwise. The cart_keys will be used in an foreach statement to extract each key id and put it in the session call statement as in $_SESSION["cart_array$key"] to then show number of items ordered. Just about got this working.  I'm sure you are also trying to figure this out in your own way, but this is what I've come up with to get items and quantity saved for each item to session. Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 30, 2011 Share Posted May 30, 2011 This is what I have working. session_start(); if (!isset($_SESSION["cart_keys"])){ $_SESSION["cart_keys"]=array(0); } IF ($_POST['button']=="Add to Cart"){ foreach($_POST as $ky => $val){ IF ($ky!="button"){ $key=$ky; } } // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array$key"]) || count($_SESSION["cart_array$key"]) < 1) { Â Â // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array$key"] =array(1); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT IF (isset($_SESSION["cart_array$key"])){ Â Â $i++; } IF ($i==1){ $v=$_SESSION["cart_array$key"][0]; $nv=$v+1;Â $_SESSION["cart_array$key"] = array($nv); $wasFound = true; } } $ck=$_SESSION["cart_keys"]; if (in_array($key, $ck, true)){ // echo "Got key"; } ELSE{ array_push($ck, $key); $_SESSION["cart_keys"] =$ck; } }//end post ////Â Display //// foreach($_SESSION["cart_keys"] as $cartitem){ IF ($cartitem>0){ $iq=$_SESSION["cart_array$cartitem"][0]; echo "Item Ordered: $cartitem Quanitity: $iq<br />"; } } //////////Clear Cart for testing//////////// IF ($_GET['end']=='t'){ session_destroy(); } echo "<a href='test9.php?end=t'>Clear Cart</a><br />"; //////////// Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 aaaaaa.... I could understand things easier a year ago...! Maybe I am getting tired! Â Or maybe I am trying to understand more than my skills are allowing me to! (and I am not even avarage on php) Â I get that [key]=>must_be_unique. Â What I still didn't understand is... the $pid becomes unique, (each item displayed it has its own <form> and <button with unique value> and since the 1st item is regognized and proccessed (and created by the dynamicList also), Â where is the code stuck???? why isn't passing the rest of the items throught the add code ??????? Â 1. If the result was to add to the cart the same item each time I was clicking a different add-to-cart button, I would understand. Â 2. If it was not displaying any of the items to the cart because it couldn't see the unique $pid, I would understand. Â 3. If the items after the 1st didn't sent me to a blank page but leaving me to my products page (even without adding the item), I would partially understand. Â 4. If I was clicking for firts time any item and it was added to the cart or at least not sending me to the blank page, and all the collapse was coming after I was clicking another item, I would understand. Â What is happening... I am not! Since all of my items are equal, why the first one ONLY works? Â All the items after 1st are ignoring my js file that tells them to stay at the same page and my add to cart code. But if I add a header("location: ../p_cart.php"); all these items instead of sending me to the blank page, will take me to the cart.php. Â Drummin if you are bored with me... I WILL UNDERSTAND Â Thank you for helping me so far.... Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 RED is the key and must be unique. The value can be anything. [key]=>this can be anything. So you can't use item_is as the key. Is that clear? Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 [key]=>must_be_unique If you are refering to the above, it was a misstype. I understood that [key] should be [unique]=>whatever. Clear enough... Â It should be unique and mine is always item_id...? Is this what you mean correct? Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 So what is your current processing script? Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 My current proccessing script???? Is what I showd above. My add-to-cart.php  Or you ask if I have changed it? Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 array("item_id" => $val, "quantity" => 1)); item_id and quantity are "keys". Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 Yes... they are my keys and you told me that they should get unique name because this is the problem. Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 Did you try the code I posted? Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 yes I tried but I had undifined index error.... Undefined index: button Undefined index: end Undefined varianle: i Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 Does your form have <input type="hidden" name="'. $id . '" value="pid" /> <input type="submit" name="button" value="Add to Cart" />  Looks like the code I posted is also missing the $i=0; before the line // If the cart session variable is not set or cart array is empty  AND you'll need to change this url to your page for testing echo "<a href='test9.php?end=t'>Clear Cart</a><br />"; The line below is not used and can be removed as well. Understand there were MANY versions of this script and not everything got cleaned up. $wasFound = true; Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 31, 2011 Author Share Posted May 31, 2011 Still I get the errors.... Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 31, 2011 Share Posted May 31, 2011 Well it needs to be <input type="hidden" name="'. $id . '" value="pid" /> On my copy I'm seeing results for each item added. Item Ordered: 1 Quanitity: 4 Item Ordered: 3 Quanitity: 8 Item Ordered: 6 Quanitity: 3 Clear Cart Quote Link to comment 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.