4

I am simply trying to return the rows from a table in my db but i keep getting this error thrown back at me no matter what I do "Fatal error: Unsupported operand types in C:\xampp\htdocs\d7\includes\form.inc on line 1009". Am I missing a argument or something?

This is my function

Hook menu

function auto_form_menu() {
$items = array();
$items['admin/config/auto_form'] = array(
    'title' => 'My Module',
    'description' => 'description',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('auto_form_records'),
    'access arguments' => array('administer site configuration'),
    'file' => 'auto_form.admin.inc'
);

$items['admin/config/auto_form/manage'] = array(
    'title' => 'My Module',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'description' => 'description',
    'weight' => -10,
);
return $items;
}

'auto_form.admin.inc'

function auto_form_records() {
  $header = array('First Name', 'Last Name');

  $dbresult = db_query("SELECT first, last FROM {auto}");

    $rows = array();
    foreach ($dbresult as $dbrow) {
        $rows[] = array(
        $dbrow->first,
        $dbrow->last
      );
    }
    return $output = theme('table', array('header' => $header, 'rows' => $rows));
}

Updated Code:

Any help is appreciated seeing how I am completely lost on figuring this out.

1

I'm not sure exactly which one is causing the error but there are a couple of things:

  • You're trying to loop over a variable called $dbresult which you haven't declared; you probably meant

    $dbresult = db_query("SELECT first, last FROM {autoform}");
    
    $results = '';
    foreach ($dbresult as $dbrow) {
    
  • It looks like you're using Drupal 7, and the call you're making to theme_table contains arguments in the style of the Drupal 6 call. Variables are all named in Drupal 7 so you need to invoke the function like this:

    $output = theme('table', array('header' => $header, 'rows' => $data));
    
  • theme_table() takes an array of rows, and each of those arrays will have the data for a number of columns, one for each that you've defined in your header. At the moment you're concatenating a single string and creating a single row containing one cell, with that single string in it.

    I think something like this is more what you're after:

    $rows = array();
    foreach ($dbresult as $dbrow) {
      $rows[] = array(
        $dbrow->first,
        $dbrow->last
      );
    }
    
    $output = theme('table', array('header' => $header, 'rows' => $rows));
    

Also if you want a nice little shortcut leveraging some of the db API you could use this reduced code:

$header = array('first' => 'First Name', 'last' => 'Last Name');
$rows = db_query("SELECT first, last FROM {autoform}")->fetchAllAssoc();
$output = theme('table', array('header' => $header, 'rows' => $rows));

That last snippet will become less useful the more you need to customise the individual rows/cells of the table.

  • I apologized for the example I got mixed up with some of the other coding that I pasted. I tried your 1st example and some how I am still getting the same error, could it possibly something in my hook_menu? – Jeremy Love Oct 6 '12 at 1:24
  • Did you fix the other problems up as well? – Clive Oct 6 '12 at 15:23
  • Yes, updated code to show you the current selected code im using – Jeremy Love Oct 7 '12 at 19:41
0

I finally figured out what it was, it was hook_menu in my module. It was looking for a form when there was no form in place by using drupal_get_form as a page callback. If i change the page callback to the function being used in the file the error goes away and the results come back. Please see the example below...

Hook menu

function auto_form_menu() {
$items = array();
$items['admin/config/auto_form'] = array(
    'title' => 'My Module',
    'description' => 'description',
    'page callback' => 'auto_form_records',
    'access arguments' => array('administer site configuration'),
    'file' => 'auto_form.admin.inc'
);

$items['admin/config/auto_form/manage'] = array(
    'title' => 'My Module',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'description' => 'description',
    'weight' => -10,
);
return $items;
}

Function in my inc file

function auto_form_records() {
  $header = array('First Name', 'Last Name');

  $dbresult = db_query("SELECT first, last FROM {auto}");

    foreach ($dbresult as $dbrow) {
        $rows[] = array(
        $dbrow->first,
        $dbrow->last
      );
    }
    return $output = theme('table', array('header' => $header, 'rows' => $rows));
}

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.