Re: Foreach

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

 



Yes, each variable is an array. foreach works individually with no problems.
The problem I am having is getting both to update the table in MySQL. It
will update  $i three times with no problem, however, it will only update $t
with the last value in the array.

Name is a checkbox and contains the name of the record from the db. Order is
a select menu with a number. (which is the number drawn from the number of
records in the db). I would like to see both values come across and update
the table however many times necessary. Name would update the name and order
would update order. Order is the order in which each record will appear on
the page.

I hope this makes more sense. I wish I knew more, and then I would be able
to formulate my questions better. Thank you so much for your help.

Steve M.

on 1/17/08 10:07 PM David Giragosian (dgiragosian@xxxxxxxxx) wrote:

> On 1/17/08, mike <mike503@xxxxxxxxx> wrote:
>> On 1/17/08, Nathan Nobbe < quickshiftin@xxxxxxxxx
>> <mailto:quickshiftin@xxxxxxxxx> > wrote:
>> 
>>>>  $name = $_POST['name'];
>>>>  if ($name) {
>>>>  foreach ($name as $t) {
>>>> 
>>>>        echo "$t";
>>>>    } 
>> 
>>>>    $order = $_POST['order'];
>>>>    if ($order) {
>>>>    foreach ($order as $i) {
>> 
>>> 
>>> there are a few different issues here; first of all; are you sure
>>> $_POST['name'] 
>>> and $_POST['order'] are even arrays?
>> 
>> hint:
>> 
>> if(isset($_POST['name']) && is_array($_POST['name']))
> 
> Steve,  
> 
> // Do you have several html form elements such as <input type="text"
> name="name[]"> in your html?
> // Mike's suggestion...
> if( isset( $_POST['name'] ) && is_array( $_POST['name'] ) ) {
> 
> // you'll never get in here if you don't...
> $name = $_POST['name'];
>          
>        // foreach expects an array, as Nathan states. Even if $name is an
> array, $t 
>        // will hold only the last value in the array when the foreach loop is
> exited
>        // because $t is being overwritten with each iteration.
>        foreach ($name as $t) {
> 
>                echo "$t";
> 
>        } // end foreach ($name)
> 
>        $order = $_POST['order'];
>        
>        if ($order) {
>                
>                // see above about arrays and foreach
>                foreach ($order as $i) {
> 
>                       //Update the table in MySQL
> 
>                       $i = mysql_real_escape_string($i, $cnx); // One of
> Eric's suggestions
> 
>                       $update_data = "UPDATE sections SET `order` = '$i' WHERE
> name = '$t'";
> 
>                       $response = mysql_query( $update_data, $cnx );
> 
>                       if(mysql_error()) die ('database error<br>'.
> mysql_error());
> 
>                       echo "$i";
> 
>                } //end foreach ($order)
> 
>        }
> 
> }
> 
> Assuming both $_POST['name'] and $_POST['order'] are arrays, the way your code
> is now structured, the table `sections` will have the record(s) where name
> equals the last value in the $names array updated multiple times, once for
> each value in the $order array, but all you will see is that the record(s)
> will have the last value in the $order array.
> 
> See if this makes any sense and then ask more questions.
> 
> David
> 

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