Jump to content

foreach when using 2 arrays


graham23s

Recommended Posts

Hi Guys,

 

In a form i have 2 inputs:

 

<td align=\"center\"><input type=\"hidden\" name=\"pid[]\" value=\"$product_id\"><input type=\"text\" name=\"q[]\" size=\"5\" value=\"$product_qty\">

 

i have put them in [] to make arrays, when submitted i catch the GET like:

 

if($_GET['action'] == 'update') 
{ 
  
  $product_id_to_update = $_POST['pid']; 
  $quantity_to_update = $_POST['q']; 
   
  foreach($quantity_to_update as $value) 
  { 
   
  // update query // 
  $queryupdate = "UPDATE `fcp_orders` SET `qty`='$value' WHERE `product_id`='$product_id_to_update'"; 
  echo $queryupdate; 
  echo ("<br />"); 
   
  } 
   
} 

 

which displays this when echoed out:

 

UPDATE `fcp_orders` SET `qty`='577' WHERE `product_id`='Array' 
UPDATE `fcp_orders` SET `qty`='277' WHERE `product_id`='Array' 
UPDATE `fcp_orders` SET `qty`='377' WHERE `product_id`='Array'  

 

which is half way there i need the product_id to be the actual integers, is there anyway i can combine the foreach to include them both at all?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/91167-foreach-when-using-2-arrays/
Share on other sites

A better way would be to do this in your form:

<?php
echo '<td align="center"><input type="text" size="5" name="pid[' . $product_id . ']" value="' . $product_qty . '">';
?>

 

Then you could do:

<?php
foreach ($_POST['pid'] as $product_id => $value) {
   $queryupdate = "UPDATE `fcp_orders` SET `qty`='$value' WHERE `product_id`='$product_id'";
   echo $queryupdate . "<br>\n";
}?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

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