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

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.