Hi all,

I have a shopping basket feature on my website which saves the product's primary key in the session array called 'cart', each one seperated by a comma, so example: '123,456,789'.

Now in order to get each item from my shopping basket to paypal I need to write 2 hidden form fields onto the page containing the product name and price, however if the user has more than one of the same item, I want it to write a hidden field for the quantity instead of simply writting the same product over and over again.

Here is the script i currently have:

$cart = $_SESSION['cart'];
$items = explode(',',$cart);
foreach ($items as $pid) //pid is the product's primary key
		$i = $i + 1;
		$pricequery = mysql_query('SELECT * FROM products WHERE pid = "'.$pid.'"');
		while($price = mysql_fetch_array($pricequery))
			echo'<input type="hidden" name="item_name_'.$i.'" value="'.$price['name'].' '.$price['series'].' '.$price['manufacturer'].'"><input type="hidden" name="amount_'.$i.'" value="'.$price['price'].'"><input type="hidden" name="quantity_'.$i.'" value="'.$qty.'">';

As you can see from the code, $qty has not been set. This is where I am having my difficulty. I was thinking for each product you could add it's PID into an array then check if the pid is in the array before deciding wether or not to write the quantity, however the problem occurs when having to go back and change the quantity field that has been echo'd because another product with the same PID has been found in the cart array.

Sorry for the lenghty description etc! I hope somebody has the time to help, so thanks in advance.


9 Years
Discussion Span
Last Post by MDGM

dont worry, iv figured it out. Do something like this:

explode the cart into an array.
foreach($cart as $pid)
if the pid is in the array, do nothing
else: use substr_count to get the QTY
add pid to array

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.