Pagination for entities I have an entity called announcement, and I have a page (user/%/announcement) where I display a table of three columns (name of entity,an edit entity link and an delete entity link).

function announcements_user($account) {
  $uid = $account-&gt;uid;
  $return = '';
  $total = 0;
  $i=0;
  $pid = array();
  $text = array();

  $query = db_select('announcements','a')-&gt;extend('PagerDefault');
  $query-&gt;fields('a',array('pid','value'));
  $query-&gt;condition('uid', $uid);
  $result = $query-&gt;execute();

  foreach($result as $row) {
    $pid[] = $row-&gt;pid;
    $text[] = $row-&gt;value;  
    $total++;
  }

  $return .= '&lt;table border="1"&gt;
    &lt;tr&gt;
     &lt;th&gt;Announcement&lt;/th&gt;
     &lt;th&gt;Edit&lt;/th&gt;
     &lt;th&gt;Delete&lt;/th&gt;
    &lt;/tr&gt;';

  while ($total) {
    $return .= '&lt;tr&gt;';
    $return .= '&lt;td&gt;&lt;a href="'.$base_url.'/announcement/'.$pid[$i].'"&gt;'.substr($text[$i],0,10).'......&lt;/a&gt;&lt;/td&gt;';
    $return .= '&lt;td&gt;&lt;a href="'.$base_url.'/announcement/edit/'.$pid[$i].'"&gt;Edit&lt;/a&gt;&lt;/td&gt;';
    $return .= '&lt;td&gt;&lt;a href="'.$base_url.'/announcement/delete/'.$pid[$i].'"&gt;Delete&lt;/a&gt;&lt;/td&gt;';
    $return .= '&lt;/tr&gt;';
    $i++;
    $total--;
  } 

  $return .='&lt;/table&gt;';
  $return .='&lt;br&gt;';
  $return .='&lt;a href ="'.$base_url.'/announcement/add"&gt;Add Announcement&lt;/a&gt;';

  return $return;   
}


Now my problem is that keeping this format of tabular structure, how do I display my result in a paginated format with 5 announcements per page? I have seen pager.inc for Drupal 7 and howto-print-out-the-pagination-for-entityfieldqueries link, but I just can't figure out how to apply them to my case.
function announcements_user($account) {
  $header = array(
    'announcement' =&gt; array(t('Announcement')),
    'operations' =&gt; array('data' =&gt; t('Operations'), 'colspan' =&gt; '2'),
  );
  $rows = array();

  $result = db_select('announcements','a')
    -&gt;fields('a', array('pid','value'))
    -&gt;condition('uid', $account-&gt;uid)
    -&gt;extend('PagerDefault')
    -&gt;limit(5)
    -&gt;execute();

  foreach ($result as $data) {
    $row = array();
    $row[] = l(truncate_utf8($data-&gt;value, 10, FALSE, TRUE), 'announcement/' . $data-&gt;pid);
    $row[] = l(t('Edit'), 'announcement/edit/' . $data-&gt;pid);
    $row[] = l(t('Delete'), 'announcement/delete/' . $data-&gt;pid);

    $rows[] = $row;
  }

  $form['announcements'] = array(
    '#theme' =&gt; 'table', 
    '#header' =&gt; $header, 
    '#rows' =&gt; $rows, 
    '#empty' =&gt; t('No announcement available.'),
  );

  $form['pager'] = array('#markup' =&gt; theme('pager'));
  return $form;
}



Query methods generally return the object for which they are invoked; this means that method calls can be concatenated as I did in  the code.
PagerDefault::limit() has a default limit of 10; if you don't call that method, the limit is the default value.
Strings that appear in the user interface should always be translated; the only strings that cannot be translated (which means, you cannot pass them to t()) are dynamic strings obtained from a function/method.
Drupal URLs should always be obtained from url() or l(). l() returns the HTML tag for links (&lt;a&gt;), while url() returns the relative or absolute URL for the Drupal path passed as argument.


For an example of function that does something similar, see blog_page_last(), which returns the last blog posted by a user.