Re: Foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Steve,

This should work as some basic sudo code.  You are running into a number of issues with your usage of the foreach as it sounds like what you really want to do is walk through one array and grab the corresponding value from another.

<?php
// First check to make sure you are getting both fields
if(isset($_POST['name']) && is_array($_POST['name']) && isset($_POST['order']) && is_array($_POST['order']))
{
 // Now assign them to easier to play with variables
 $names=$_POST['name'];
 $orders=$_POST['orders'];
 // This tests for the same number of items as names
 if (count($names) == count($orders)) 
 { 
  $i=0;
  while($i<=count($names))
  {
   $update_data = "UPDATE sections SET `order` = '$orders[$i]' WHERE name = '$names[$i]'";
   $response = mysql_query( $update_data, $cnx );
   if(mysql_error()) die ('database error<br>'.mysql_error());
  }
 }
}
?>

HTH,
Wolf

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux