0

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.

2
  • 1
    you aren't outputting anything in foreach loop Commented Mar 5, 2014 at 14:57
  • i use foreach in foreach this part is ok , my problem is $options = array("img","siteurl"); just showing last values in ` $result2[$value] = array( $options_key => $_POST[$options_key][$value] );` Commented Mar 5, 2014 at 15:01

1 Answer 1

2
$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];
Sign up to request clarification or add additional context in comments.

3 Comments

thanks you a lot , can you explain about this if( !isset($result2[$value])) and if possible i will be thankful to explain me why my code doesn't work , because i think my loops was true
Your code repeatedly overwrites the $result2[$value] array with a new array that has a single key - the last one. My code will initialise the array if it doesn't already exist, and then add a key to it instead of completely replacing it.
@Niet if( !isset($result2[$value])) $result2[$value] = array(); is completely unnecessary, right? This answer is missing its educational explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.