Pass multiple ids from product page to cart page

hi all

at present i am adding single product to cart via href


<?
<a href="cart.php?product_id=1"><img src="addtocart.gif"></a>
?>

now i want to add “product 1” + “related_products with checkboxes” if their checkboxes are checked.


<input type="checkbox" value="2" name="related[]" />
<input type="checkbox" value="3" name="related[]" />
<input type="submit" value="addtocart">

i can get checkboxes values in array()


<?
$related_all = array();
$related_all = $_REQUEST['related'];
?>

how will i pass all these 3 values to cart.php and what will my below insert code be changed to


<?
if(isset($_REQUEST['product_id']))
{
	$qry = "Insert into cart_table"
}
?>

vineet

hi tried this but it outputs id as Array


$related_all = array();
$related_all = $_REQUEST['related'];

foreach($_REQUEST['related'] as $product_id)
{


	if(isset($_REQUEST['related']))
	{
	$id=$_REQUEST['related'];

	echo $id; /* ouput as ArrayArrayArray */
	
	$qry = "select * from product_table where product_id = $id";
	
echo $qry; /* output = where product_id = Array */

	}
}
?>

vineet

$_REQUEST[‘related’] is not a simple single-dimensional array of values as you might think. Put this into your php code:

print_r($_REQUEST['related']);

then check some checkboxes and test it. View page source and it will become obvious to you what needs to be corrected.

ok i got it working

but dont know why this error is being displayed


Warning: Invalid argument supplied for foreach() in F:\\xampp\\htdocs

vineet

You many need to move your…

if ( isset($_REQUEST['related']) )

… ABOVE your foreach. If no boxes were checked, the “foreach” will be trying to work on something that isn’t set - and thus isn’t an array.

I think that’s right. HTH.

Jeff Cohan