i want to change values of only checked elements on tableselect. How can i do that please ?

this is my tableselect :

$form['tab'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('empty !'),

return $form;

i've also a submit button that makes changes (update database) on one column of the tableselect :

 $form['published'] = array(
        '#type' => 'submit',
        '#value' => t('Publish'),
        '#states' => array(
            'invisible' => array(
            ':input[name="select_list"]' => array('value' => 'Published'),
        '#submit'=> array('publish_submit'),

My function to make changes (to publish elements that aren't now) :

function publish_submit($form, &$form_state){
$check = $form_state['values']['tab'];
//$check = array_filter($form_state['values']['tab']);

$sql = db_update('field_data_field_books');
$sql->fields(array('field_books_value'=> 'Published')); 
$sql->condition('entity_id',$check); //update only books i checked on the tableselect
$query = $sql->execute();

this is what dsm($check) returns (when i check one or more elements on the tableselect )

enter image description here

this is dsm($rows) (6 elements are ok):

enter image description here

enter image description here

This is how i created the $rows :

$rows = array();

foreach ($result as $res){

    $rows [] = array(

            'book_title' => l($res-> field_title_value, 'node/'.$res->entity_id),
            'book_state' => $res-> field_state_value,
            'book_editor' => $res-> field_editor_value,
            'book_categories' => $res-> name,
            'edit_books'=>l(t('Details'), 'node/'.$res->entity_id.'/edit'),


$form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#multiple' => TRUE,
    '#empty' => t('nothing!'),

  • Well first you should check the structure of $check variable. You can just var_dump($check); die(); or use something like devel module and dpm($check);. It needs to be a simple array of id's. Then you need to put the condition as "IN" like so: $sql->condition('entity_id', $check, 'IN');. That would updated the field_books_value to Published on all id's contained in $check. See "Array Operators" on the Conditional Clauses documenation page. – Beebee Jun 28 '15 at 10:51
  • Thnx beebee, first i've changed : $check = array_filter($form_state['values']['tab']); to $check = $form_state['values']['tab']; and when i do dsm($check), it returns : ... (Array, 9 elements) 0 (String, 1 characters ) 0 (this is because i checked the first item) 1 (Integer) 0 2 (Integer) 0 do you think i'm getting the real checked value ? – Mitch Jun 28 '15 at 10:57
  • please check the picture i've just added bellow – Mitch Jun 28 '15 at 11:05
  • dsm($rows); and put 1 row's structure in the question too. I suppose you you looked at the #tableselect Form API reference – Beebee Jun 28 '15 at 11:11
  • 1
    You need to show us how you're creating $rows that gets assigned to the tableselect as #options. This $rows array needs to be an associative array, associated by the entity's ID. So if you're including node with id 321 in the array it should read: $rows[321] = array(...);. Then tableselect should give you the correct ID's in $check. Please put how you created $rows in the question. – Beebee Jul 6 '15 at 13:17

As beebee said on comments :

$rows should be indexed like this :

foreach ($result as $res){

    $rows [$res -> entity_id] = array(

and the condition of the publishing function :

function publishing_submit($form, &$form_state){

    $check = array_filter($form_state['values']['table']);
    $sql = db_update('field_data_field_state');
    $sql->fields(array('field_state_value'=> 'Published'));
    $sql->condition('entity_id', $check, 'IN');

Thanx beebee

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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