0

i am adding custom modules in drupal 7. while creating a custom table i got the error like this "Fatal error: Cannot use string offset as an array in /srv/bindings/code/includes/theme.inc on line 2002"

            $form = array();
            $headers = array(t('Teacher Name'), t('Paypal Id '),  t('Status'), t('Action'));
            $query = "SELECT * FROM `users_roles` ur JOIN `users` u ON u.uid=ur.uid WHERE rid=4";
            $rs = db_query($query);
            $row = array();
            if ($rs) {
                  foreach($rs as $data) {
                    $name = $data->name;
                    $mail = $data->mail;
                    $status = $data->status ;
                    $row[] = array(stripslashes(ucwords($data->name)), $mail, $status, 
                    "<a href='#'>" . t('Pay Now') . "</a>  " );
                }}
                echo "<pre>";
                print_r($headers);
                print_r($row);
            return theme_table('table', array('header' => $headers, 'rows' => $row));

I dont know where i done mistake. Please give some ideas.

  • This error happens when you try to access a variable like an array when the variable was initialised with a different type than array. That was clear as the PHP Error discribes the problem. – Ashish Deynap Jan 3 '17 at 5:48
1

You're calling theme_table directly instead of theme, but still passing the name in.

Use this instead:

return theme('table', array('header' => $headers, 'rows' => $row));

Or even better (depending on context), a render array.

0
        $form = array();
        $headers = array(t('Teacher Name'), t('Paypal Id '),  t('Status'), t('Action'));
        $query = "SELECT * FROM `users_roles` ur JOIN `users` u ON u.uid=ur.uid WHERE rid=4";
        $rs = db_query($query);
        $row = array();
        if ($rs) {
              $result = $rs->fetchAll();
              foreach($result as $data) {
                $name = $data->name;
                $mail = $data->mail;
                $status = $data->status ;
                $row[] = array(stripslashes(ucwords($data->name)), $mail, $status, 
                "<a href='#'>" . t('Pay Now') . "</a>  " );
            }}
            echo "<pre>";
            print_r($headers);
            print_r($row);
        return theme_table('table', array('header' => $headers, 'rows' => $row));

Now, it will work correctly for you.

  • Hi ashish, thanks for your comment. But still i got error like this "atal error: Cannot use string offset as an array in /srv/bindings/c90e433b09414ab9a209102a3246bcae/code/includes/theme.inc on line 2002" – vignesh r Jan 3 '17 at 6:00
  • please verify the code now. I have edited my code. – Ashish Deynap Jan 3 '17 at 6:04
  • yes i changed "foreach($result as $data) {" this – vignesh r Jan 3 '17 at 6:07
  • Are you still getting that error? – Ashish Deynap Jan 3 '17 at 6:08
  • yes ashish still error is coming – vignesh r Jan 3 '17 at 6:12
0

You don't report exactly which line is causing the error, but if it is between the lines you shown, then the line causing the error is the following one.

return theme_table('table', array('header' => $headers, 'rows' => $row));

It happens because theme_table(), as any other theme function, is expecting an array as first argument, not a string.

The correct line would be the following one.

return theme('table', array('header' => $headers, 'rows' => $row));

Keep in mind that on Drupal you never directly call a theme function like theme_table() because the theme function for generating a table could not be that. In fact, modules can change the theme function called from theme() by implementing hook_theme_registry_alter().

Then, in Drupal 7 is possibile to return a render array, instead of a string. Instead of return theme_table('table', array('header' => $headers, 'rows' => $row)); you can use return array('#theme' => 'table', '#header' => $headers, '#rows' => $row); in most of the cases.

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.