I have for some time now struggled with the database api and have now come near the goal of my database application. I have produced a couple of custom tables and are using the theme function for tables to list the data on the page. What´s left is some pages for deleting old record easily. For this I have tried to use tableselect. I have looked around and trying to do as in the example but I can not make it work.
Here is an example of a small table and the code to list this on a page:

function clubs_page() {
  $header = array(
    'clubid' => t('Clubid'),
    'clubname' => t('Clubname'),
  );
  $sql = "SELECT * FROM {clubs}";
  $result = db_query($sql);
        foreach ($result as $dbrow) {
            $rows[] = array(
            $dbrow->clubid,
            $dbrow->clubname,
            );
        }  
 $output=theme('table', array('header' => $header, 'rows' => $rows)) . theme('pager');	
  return $output;
}

This works OK.

To change this to a select page I did this changes:

function clubs_select_form() {
  $header = array(
    'clubid' => t('Clubid'),
    'clubname' => t('Clubname'),
  );
  $sql = "SELECT * FROM {clubs}";
  $result = db_query($sql);
        foreach ($result as $dbrow) {
            $rows[] = array(
            $dbrow->clubid,
            $dbrow->clubname,
            );
        }  
    $form['clubs_select'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
	  '#empty' => t('No clubs.'),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );

  return $form;  
}

Obviously this is wrong. The result is that it flags for empty table and I get this error message:
Notice: Undefined index: #attributes in theme_tableselect() (line 3332 of /home/dev_site/includes/form.inc).

Hope someone can tell me what I have done wrong

Comments

jaypan’s picture

Contact me to contract me for D7 -> D10/11 migrations.

chrotto’s picture

Thanks,
But sorry to say I do not get it work.
I even copy and paste the entire code from your tutorial but I do not get any records and this error message was displayed:

Notice: Undefined index: #attributes in theme_tableselect() (line 3332 of /.../includes/form.inc).
Warning: Missing argument 1 for my_form() in my_form() (line 969 of /.../my.module).
Warning: Missing argument 2 for my_form() in my_form() (line 969 of /.../my.module).

Line 3332 in form.inc is:

  return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $element['#empty'], 'attributes' => $element['#attributes']));

Line 969 is:

function my_form($form, $form_state)

Is there any missing part in my installation?
I am running Drupal 7.22 and PHP 5.3.25

jaypan’s picture

Always show your code.

Contact me to contract me for D7 -> D10/11 migrations.

chrotto’s picture

In this case the code (function) is an exact copy of the tutorial you linked to.

goofus’s picture

Hello,
As asked in the other replies, you need to post your code.

In addition, have to tested your sql. Have you run the sql from the database command line? How many rows and columns are in the database result?

Good Luck :)

chrotto’s picture

I now have installed a clean Drupal 7.22 site with only one custom mudule on it to test the Tutorial code and hopefully learn to understand the concept.
The module called tableselect have two files:
the tableselect.info and the tableselect.module file the code comes here:

<?php

 /**
 * Define the user permission.
 */
function tableselct_permission() {
  return array(
     'view tableselect' => array(
      'title' => t('Tableselect'),
      'description' => t('Tableselect.'),
    ),
   
  );
} 
 
/**
 * Implements hook_menu().
 */
function tableselect_menu() {
    $items['my_form'] = array(
    'title' => t('Select table example'),
    'description' => t('Select table tutorial'),
    'page callback' => 'my_form',
    'access callback' => 'user_access',
	'access arguments' => array('view tableselect'),
  );  
  return $items;
  }
// The tutorial function  
function my_form($form, $form_state)
{
  $users = array
  (
    array('uid' => 1, 'first_name' => 'Indy', 'last_name' => 'Jones'),
    array('uid' => 2, 'first_name' => 'Darth', 'last_name' => 'Vader'),
    array('uid' => 3, 'first_name' => 'Super', 'last_name' => 'Man'),
  );
 
  $header = array
  (
    'first_name' => t('First Name'),
    'last_name' => t('Last Name'),
  );
  $options = array();
  foreach($users as $user)
  {
    $options[$user['uid']] =array
    (
      'first_name' => $user['first_name'],
      'last_name' => $user['last_name'],
    );
  }
  $form['table'] = array
  (
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No users found'),
  );
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

It produce an empty table an this this errormessage:

Warning: Missing argument 1 for my_form() in my_form() (line 30 of C:\xampp_old\htdocs\tableselect\sites\all\modules\tableselect\tableselect.module).
Warning: Missing argument 2 for my_form() in my_form() (line 30 of C:\xampp_old\htdocs\tableselect\sites\all\modules\tableselect\tableselect.module).

After a refresh it also generate this error:
Notice: Undefined index: #attributes in theme_tableselect() (line 3332 of C:\xampp_old\htdocs\tableselect\includes\form.inc).

heine’s picture

drupal_get_form should be the page callback, my_form goes in page arguments.

heine’s picture

Each row must be indexed on the header keys. Every row in rows must be indexed on a unique id that ends up in the submit function. Next sample assumes clubid is that field:

$rows[$dbrow->clubid] = array(
  'clubid' => $dbrow->clubid,
  'clubname' => $dbrow->clubname,
 );

Also, make sure to change the page callback to drupal_get_form, and pass the form callback as one of the page arguments.

chrotto’s picture

Thank you!
I have now managed to get a tableselect working with my club example.
Here is the code that worked:

function my_clubs_select_form($form, &$form_submit) {
  $header = array(
    'clubid' => t('Clubid'),
    'clubname' => t('Clubname'),
  );
  $sql = "SELECT * FROM {tableselect_clubs}";
  $result = db_query($sql);
  foreach ($result as $dbrow) {
    $rows[$dbrow->clubid] = array(
    'clubid' => $dbrow->clubid,
    'clubname' => $dbrow->clubname,
     );
   }  
    $form['clubs_select'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
      '#empty' => t('No clubs.'),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );

  return $form; 
}