Pavlos1316 Posted May 26, 2011 Share Posted May 26, 2011 Hello, I am using this dynamicList to display items in my page: $sql = mysql_query("SELECT * FROM products WHERE category='body' ORDER BY id ASC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $i=0; $dynamicListBody = '<table width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;>'; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["product_name"]; $details = $row["details"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $dynamicListBody .= ($i==0) ? '<tr>':''; $dynamicListBody .= '<td width="10%"> <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" /> </td> <td width="35%"> <span class=itmttl>' . $product_name . '</span> <br /> <span class=text>' . $details . ' <br /> €' . $price . '</span> <br /> <form id="bd_itm" name="bd_itm" method="post" action=""> <input type="hidden" name="pid" id="pid" value="'. $id . '" /> <input type="submit" name="button" id="button" value="Add to Cart" /> <br /> <br /> </form> </td>'; $dynamicListBody .= ($i==1) ? '</tr>':''; $i++; ($i==2) ? $i=0:''; } $dynamicListBody.='</table>'; } else { $dynamicListBody = "We have no products listed in our store yet"; } mysql_close(); ?> How can I assign unique id & name values to my button code so it will be unique for each item???? I need it unique because I am calling that pid in my add.To.Cart.function after and is not working as is. <input type="hidden" name="pid" id="pid" value="'. $id . '" /> <input type="submit" name="button" id="button" value="Add to Cart" /> Thank you Quote Link to comment Share on other sites More sharing options...
xyph Posted May 26, 2011 Share Posted May 26, 2011 Not working as in? Error messages? Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 26, 2011 Author Share Posted May 26, 2011 not working as it is.... No error massages... If I press the first add to cart button... It Works. If I press any other, it sends me back to my homepage with no erros.... oh... I use this jquery so my page won't refresh or send me to cart.php when I click AddToCart. For this I need unique ids I think: $('#button').click(function() { $.post('help_scripts/cart_functions.php', { pid : $('#pid').val() }, function(rsp) { // make your php script return some xml or json that gives the result // rsp will be the response }); return false; // so the page doesn't POST }); Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 You're going to have to swap name and value so the unique id is for the name and pin is the value. On the pick up side of your post you will need to use a foreach statement to pick up the $key value. Something like this. foreach($_POST as $key => $val){ //DO SOMETHING WITH THIS $key ID// } Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 You're going to have $_POST['button'] as well so you might qualify $key before doing anything with it. I'm sure more qualified coders will have a more efficient code but try this. foreach($_POST as $key => $val){ IF (is_int($key)){ //DO SOMETHING WITH THIS $key ID// } } Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 Hello, On the pick up side of your post You mean I include my whole add-to-cart.code (below) inside that foreach.code? 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(); } Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 i have done this so I can detect click with jquery OLD: <input type="hidden" name="pid" id="pid" value="'. $id . '" /> NEW: <input type="hidden" name="pid" id="pid_<?php echo $id?>" value="'. $id . '" /> Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 no... it doesn't do anything different plus now it doesn't add the first itm either. Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 another thing you can try is to see if the id's are attaching themselves like you want for ex <input type="hidden" name="pid" id="pid_<?php echo $id?>" value="'. $id . '" title="pid_<?php echo $id?>" /> would allow you to hover over it and see the id if the id's are correct i would assume the problem is somewhere else once your id's are correct and unique its a matter of detecting the click im not sure what your using but when I do it in jquery i use the live function for dynamic tables Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 i see your using jquery you can adapt this example, the jquery you showed in the 2nd post will not detect a dynamic list <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]); // do something }); </script> Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 this can also be used $('#yourtablename input[id^=pid_]:not(.ui-pid_)').live("click",function(){ $('#yourtablename input[name^=pid_]:not(.ui-pid_)').live("click",function(){ $('#yourtablename select[id^=pid_]:not(.ui-pid_)').live("click",function(){ depending on if you use a id, name or <select > dropdown list Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 Hey forgive me Pavlos for not reading your code carefully enough. I thought you were posting several items at a time. Please disregard my earlier posts. Just looking at your first code, I get "white space errors" because of the line returns. I removed those returns and formatted in a style I would use. Maybe this will work for you. $sql = mysql_query("SELECT * FROM products WHERE category='body' ORDER BY id ASC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $i=0; $dynamicListBody = "<table width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["product_name"]; $details = $row["details"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $dynamicListBody .= ($i==0) ? '<tr>':''; $dynamicListBody .= "<td width=\"10%\"><img style=\"border:#666 1px solid;\" src=\"../stock_photos/$id.png\" alt=\"$product_name\" /></td><td width=\"35%\"><span class=itmttl>$product_name</span><br /><span class=\"text\">$details<br />€$price</span><br /><form id=\"bd_itm\" name=\"bd_itm\" method=\"post\" action=\"#\"><input type=\"hidden\" name=\"pid\" value=\"$id\" /><input type=\"submit\" name=\"button\" value=\"Add to Cart\" /><br /><br /></form></td>"; $dynamicListBody .= ($i==1) ? '</tr>':''; $i++; ($i==2) ? $i=0:''; } $dynamicListBody.='</table>'; } else { $dynamicListBody = "We have no products listed in our store yet"; } mysql_close(); Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 Where did you saw the white space errors???? (because I don't have them... I will give it a try.... You just put all in a straight line and escaping quotes? Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 Let's back up. In both versions I see the pid value for each item. When posting, is this value not being picked up? Adding print_r($_POST); I see the id being passed. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 I will shoot myself... I will shoot myself... I will shoot myself... I will shoot myself... I will shoot myself... I was testing what you and gevensen telling me to..... but I just noticed that I was testing without proccesing my form... <form id="bd_itm" name="bd_itm" method="post" action=""> I must of left the action empty in one of my copy/paste.... Goooooood.... I have to test what you and gevesen advised me, all over again!!!!!!! :'( :'( stupid... stupid... stupid... me!!!! Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 I feel pretty bad about my posts as well. The white space error was my fault as I had placed your code inside a page I already had to access a DB table already defined. I messed up. You do have empty <span class=itmttl></span> and duplicate id="bd_itm", id="pid" and id="button" for each item and the form action is not defined. But as far as the original issue of id not being passed, that seems fine. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 When posting, is this value not being picked up? According to this: if (isset($_POST['pid'])) { $pid = $_POST['pid']; yes pid is posting... In both versions I see the pid value for each item Both versions???? your first though, (even if you say that it was for several item at a time) seems more logic to me.... Which part of my code should I put inside //DO SOMETHING WITH THIS $key ID// so I test it again: foreach($_POST as $key => $val){ IF (is_int($key)){ //DO SOMETHING WITH THIS $key ID// } } You do have empty <span class=itmttl></span> This I don't see... I think I don't have it... duplicate id="bd_itm", id="pid" and id="button" Now this is what I am trying to change To get unique values in case this is my error... Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 duplicate id="bd_itm", id="pid" and id="button" Now this is what I am trying to change To get unique values in case this is my error... I don't think you need these html id's at all. You just need to process the $_POST['pid'] and add it to the cart. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 I will put everything in an order (because I messed up this post) Sorry if I stunned you. I have my: 1. dynamic list Using it to display my items plus my ADD BUTTON for each item: <form id="bd_itm" name="bd_itm" method="post" action=""> <input type="hidden" name="pid" id="pid" value="'. $id . '" /> <input type="submit" name="button" id="button" value="Add to Cart" /> 2. add-to-cart function This is clear. 3. jquery Using it to be able to add an item without having the page being refreshed or sending me to the cart.php The problem... The first item in my list is added in the cart normaly with no refreshing etc... Any other item will also get added to the cart BUT resulting to a Blank Page MY PROBLEM. Now the most likely cause for this (as I suspect), is that I am not rendering my button (and maybe my form also) with Unique ID. [or not? I don't know any more] *If you like I can repost the codes also (in an order this time) Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 Well I don't know much about jquery but if you need the form id="bd_itm" to process, then you'll need to have a unique value. By just adding $id to the end should give you a unique id for each as in id="bd_itm'. $id . '" though not sure if that will be what you need. Personally I'd just do a straight post to a processing page and direct them back to the product page, which will re-query and show items in their cart. Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 You might also want to add "style=" to your table. <table style='width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;'> Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 join the club Quote Link to comment Share on other sites More sharing options...
gevensen Posted May 27, 2011 Share Posted May 27, 2011 Well I don't know much about jquery but if you need the form id="bd_itm" to process, then you'll need to have a unique value. By just adding $id to the end should give you a unique id for each as in id="bd_itm'. $id . '" though not sure if that will be what you need. Personally I'd just do a straight post to a processing page and direct them back to the product page, which will re-query and show items in their cart. once you get past the what in the world am i doing jquery basically supercharges php, no more page refreshes unless you want to Quote Link to comment Share on other sites More sharing options...
Drummin Posted May 27, 2011 Share Posted May 27, 2011 Hey well, sorry I wasn't much help. This topic was here for days without anyone responding so I thought I see about it. I will stay out and let those more qualified respond. Quote Link to comment Share on other sites More sharing options...
Pavlos1316 Posted May 27, 2011 Author Share Posted May 27, 2011 Drummin... I will try the form id variation also... as for style... That I know Hey well, sorry I wasn't much help. This topic was here for days without anyone responding so I thought I see about it. I will stay out and let those more qualified respond. Don't say that... Any try to help is appreciated!!!! Nobody knows everything... Thats why we ask, and we play around with the codes. gevensen... since you seem to know quite a lot about jquery, where do you think is my promblem? I will try again what you previwsly posted (if you read few posts ago I didn't proccesing my form, when I was trying your solutions.) 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.