8

I would like to create a pager in the table when there is fix amount of rows.

The method suggested on internet creates table, and it fetches data by using sql query, but i want to create a table from the data from the array.

visitedgroups = array('1','2','3','4');//It is an array of node id which are group entity
$header = array(
                array('data' => t('Group Name')),
                array('data' => t('Group Since')),
                array('data' => t('Join')),
            );
            $rows = array();
            foreach ($visitedgroups as $groupid) {
                $groupnode = node_load($groupid);
                $createdtime = format_interval((time() - $groupnode->created) , 2) . t(' ago');
                $joinlink = l('Join Group', 'group/node/'. $groupnode->nid.'/subscribe');
                $rows[] = array( l($groupnode->title, 'node/'. $groupnode->nid),
                                                t($createdtime),
                                                $joinlink,
                                    );
            }
            $output .= t('Recently visited groups by you, in which you are not member.');
            $output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('group-visited-table')))).theme('pager');

The above code generates the table and working fine. But i want to add pager in the table. From the references on internet i found that people adds,

$query = $query->extend('TableSort')->extend('PagerDefault')->limit(10);

ahead of $query->execute(), but i don't have query.

So how can i apply pager in this scenario..

7

Try the following function:

/**
 * An generic array pager for Drupal.
 * For Drupal 5 and 6, the default limit is 10. For Drupal 7 it is 9.
 */
function pager_array_splice($data, $limit = 9, $element = 0) {
  global $pager_page_array, $pager_total, $pager_total_items;
  $page = isset($_GET['page']) ? $_GET['page'] : '';

  // Convert comma-separated $page to an array, used by other functions.
  $pager_page_array = explode(',', $page);

  // We calculate the total of pages as ceil(items / limit).
  $pager_total_items[$element] = count($data);
  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  return array_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}

$output = pager_array_splice($yourarray, 5);  // 5 is per page values

See Paging non-SQL data for more details.

  • 2
    This is beyond useful, excellent find :) I've added the code in as i have a feeling a lot of people will want this – Clive Nov 6 '12 at 13:42
  • @RavindraSingh can you tell me where should i attach this code..as i am confused a little.. – mohit_rocks Nov 7 '12 at 4:17
  • pager_array_slice() is the core function you just nedd to use $output = pager_array_splice($yourarray, 5); // 5 is per page values in your function – Ravindra Singh Nov 7 '12 at 6:09

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.