user3325376
user3325376

Reputation: 198

foreach loop only outputs last array value in $_POST

I want to create a mass update page, so when user click on check box then edit posts to update posts.

Here is some form data come from database, I inserted here as html:

<form method="post"> 
    <table border="1">
      <tr>
         <td>Select</td>
         <td>url pic</td>
         <td>url web</td>
      </tr>
      <tr>
         <td><input type='checkbox' value='2'  name='pp[]'> 2  0  <td>
         <td><input type="text" value="lvTbHafU1L2gqnmuSVMrWZzkcGJxORFsjpg"  name="img[2]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[2]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='3'  name='pp[]'> 3  0 <td>
         <td><input type="text" value="Da0qf3yKRglNewH6X5n9zShLGubZVQtxjpg"  name="img[3]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[3]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='4'  name='pp[]'> 4  0 <td>
         <td><input type="text" value="SGdQJ8h5CjHPkEbYpF9oglatsTfyc0nA.jpg"  name="img[4]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[4]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='5'  name='pp[]'> 5  0 <td>
         <td><input type="text" value="36247u089pt51l.jpg"  name="img[5]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[5]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='6'  name='pp[]'> 6  0  <td>
         <td><input type="text" value="7a5083ou41269s.jpg"  name="img[6]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[6]"></td>
      </tr>
    </table>
   <input type="submit" name="sb" />
</form>

Now I want:

  1. Find checked checboxes - ok
  2. Get checked checkboxes values => i have a problem at this setp

Here php code:

<?php
     if (isset($_POST['sb'])) {
              $option = array("pp");
              $result = array();

              foreach ( $option as $key ) {
                  $result = $_POST[$key];   
              }

             $result2 = array();
             $options = array("img","siteurl");

             foreach ( $result as $keys => $value ) {
                foreach ( $options as $options_key ) {
                   $result2[$value] = array( $options_key => $_POST[$options_key][$value] ); // problem !
                }
             }
        }

   echo '<pre>';
   print_r ($result2);
   echo '</pre>';
?>

I have a problem in this code

 $result2[$value] = array( $options_key => $_POST[$options_key][$value] ); 

Because it shows only last value in:

 $options = array("img","siteurl");

The output is :

Array
(
    [5] => Array
        (
            [siteurl] => http://google.com
        )

    [6] => Array
        (
            [siteurl] => http://google.com
        )

)

I need in output something like this:

Array
(
    [5] => Array
        (
            [img] => 36247u089pt51l.jpg
            [siteurl] => http://google.com
        )

    [6] => Array
        (
            [img] => 7a5083ou41269s.jpg
            [siteurl] => http://google.com
        )

)

NOTE : If I use this code:

        $result2[$value] = array( 'img' => $_POST['img'][$value] ,
        'siteurl' => $_POST['siteurl'][$value]
         );

The outuput will be:

[5] => Array
    (
        [img] => 36247u089pt51l.jpg
        [siteurl] => http://google.com
    )

It will be ok, but I don't want do something like this, because I have too many values (I wrote just 2 of them in this code) and want to know how to solve this problem.

Upvotes: 0

Views: 785

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

$result2[$value] = array( $options_key => $_POST[$options_key][$value] ); // problem !

Change to:

if( !isset($result2[$value])) $result2[$value] = array();
$result2[$value][$options_key] = $_POST[$options_key][$value];

Upvotes: 2

Related Questions