Re: Foreach
| From: | Wolf | Date: | Fri, 18 Jan 2008 16:38:15 +0000 |
| Subject: | Re: Foreach | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-267566@lists.php.net to get a copy of this message | ||
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