0

I have a function mymodule_submissions in my module which fetches rows from mymodule_table and prints as table theme with pager. Its working fine if I define a menu link in my module to call that function. Pager not printing when I call the same function from a page.tpl.

function mymodule_submissions() { 

   $query = db_select('mymodule_table', 'uf');
   $query = $query->condition('uid', $user->uid)
             ->fields('mytable', array('id', 'name', 'createdtime'))
             ->orderBy('createdtime', 'DESC')->limit(3);
   $results = $query->execute();
   $header = array('ID','Name','Created Time');

   $rows = array();

   foreach($results As $result) {
      $rows[] = array( $result -> id, $result -> name, $result -> createdtime,);
   }

   $output .= theme('table', array('header' => $header, 'rows' => $rows,) );

   $output .= theme('pager');
   return $output;
}

From page.tpl, I am calling this function link below

print render(mymodule_submissions());

Anything I'm doing wrong?

1
  • 1
    I think you should really consider the power of Views for your query and your pager. From what I can see, you're using a custom table. Exposing your table to Views is quite easy (a good tut can be found here: sitepoint.com/exposing-tables-views-drupal-7)
    – Kevin
    Commented Mar 11, 2015 at 6:53

1 Answer 1

0

You just need to add ->extend('PagerDefault');

$query = db_select('mymodule_table', 'uf')->extend('PagerDefault');

I find the examples module really useful. They have a pager example.

For what its worth I think using the Drupal db API with theming is a perfectly valid way forward that has a lot of strengths: it's efficient, flexible, powerful and you can put it in version control. Obviously you should use the best thing for the situation.

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.