Jump to content

Give unique values inside dynamicList


Pavlos1316

Recommended Posts

  • Replies 72
  • Created
  • Last Reply

Top Posters In This Topic

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>	

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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#.

 

 

Link to comment
Share on other sites

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();
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />";
////////////

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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;	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.