0

I have a custom class which extends wp_list_table and show list of posts with two checkboxes in each row (one which have post ID as value for determining which posts are to be processed and other to determine should image be included for specific post). I have two functions for this

 function column_cb($item){
    return sprintf(
        '<input type="checkbox" name="%1$s[]" value="%2$s" />',
        /*$1%s*/ $this->_args['singular'], 
        /*$2%s*/ $item['ID']               
    );
}

function column_image($item){
    return sprintf(
        '<input type="checkbox" name="%1$s[]" value="%2$s" />',
         /*$1%s*/ $this->_args['singular'], 
        /*$2%s*/ 'yes'  
    );
}   

This would produce array like (ie if three posts are selected and two checkboxes for images selected)

[post] => Array ( 
          [0] => 6238 
          [1] => 6230 
          [2] => yes 
          [3] => 6224 
          [4] => yes
         ) 

But what I would like to get is something like (be it numeric or associative array)

[post] => Array 
     ( [0] => Array ([0] => 6238 [1] => yes)
       [1] => Array ([0] => 6230 [1] => '')
       [2] => Array ([0] => 6224 [1] => yes)
     )

How can I achieve this (and what if at some point I want include one more checkbox)?

5
  • Have you already tried to add [][] to the name? Hint: Set an id as well that is exactly the same as the name.
    – kaiser
    May 17, 2013 at 10:57
  • I already tried that (If I understood you well) but that gives me array like [post] => Array ( [0] => Array ([0] => 6238) [1] => Array ([0] => yes) [2] => Array ([0] => 6230) [3] => Array ([0] => 6224) [4] => Array ([0] => yes) )
    – ivan
    May 17, 2013 at 12:25
  • Is that your solution? If so, post a proper answer please.
    – s_ha_dum
    May 17, 2013 at 16:19
  • @s_ha_dum Ok, thanx for notice. I'm a beginner and not sure is there some better way to do this or what would be pro/contra this solution, but anyway I posted answer now.
    – ivan
    May 17, 2013 at 16:43
  • There is nothing wrong with the solution if it works as you need it to.
    – s_ha_dum
    May 17, 2013 at 16:44

1 Answer 1

1

I came up with this...I changed input fields to

 <input type="checkbox" id="%2$s" name="post[%2$s][]" value="%2$s" />
 <input type="checkbox" id="image_%3$s" name="post[%3$s][]" value="%2$s" />'

...and added third parameter to column_image function ($item['ID'])

function column_image($item){
return sprintf(
    '<input type="checkbox" id="image_%3$s" name="post[%3$s][]" value="%2$s" />',
     /*$1%s*/ $this->_args['singular'], 
    /*$2%s*/ 'yes',  
    /*$3%s*/ $item['ID'] 
  );
}   

and with that I get array which use post ID as index, for example

[post] => Array ( 
     [6238] => Array ( [0] => 6238 [1] => yes ) 
     [6230] => Array ( [0] => 6230 ) 
     [6224] => Array ( [0] => 6224 [1] => yes ) 
        )

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.