Add pagination to the results in custom module after split the row to many sub rows I try to build new module (Drupal 7), in some page I show the results to the user as a table like the following:

&lt;?php
    $header = 'bla bla bla'; //header come here
    $query = db_select('myTable', 't')
        -&gt;fields('t', array('field_1', 'start_date', 'end_date', 'due_date'))
        -&gt;extend('TableSort')
        -&gt;extend('PagerDefault');

    $result = $query
        -&gt;limit(10)
        -&gt;orderBy('t.startdate')
        -&gt;execute();

    foreach ($result as $row) {
        $rows[] = array('data' =&gt; (array) $row);
    }

    $form['tablesort_table'] = array(
        '#theme' =&gt; 'table',
        '#header' =&gt; $header,
        '#rows' =&gt; $rows,
        '#empty' =&gt; t('Sorry, there is no results to show'),
    );
?&gt;


and this of course works perfectly (each page has 10 rows), but for some reasons I have to split each row to multi sub row according to the start date and end date like the followin

foreach ($result as $row) {
    $timeInLoop = $row-&gt;start_date;
    while($timeInLoop &lt; $row-&gt;end_date){
        //split the row to multi row (one week for each row)
        $timeInLoop = strtotime(date("Y-m-d", $timeInLoop) . " +1 week");
        $row-&gt;due_date = date("Y-m-d", $timeInLoop);
        $rows[] = array('data' =&gt; (array) $row);
    }
}


and here is my problem:
if some row has 10 weeks and other row has 2 weeks the total rows in the page will be more than 10 rows 
how can I make them 10 rows in each page ???
thank you for your help
I find the solution here

$limit = 10;
$rows = pager_array_splice($rows, $limit);
$form['tablesort_table'] = array(
    '#theme' =&gt; 'table',
    '#header' =&gt; $header,
    '#rows' =&gt; $rows,
    '#empty' =&gt; t('Sorry, there is no results to show'),
);


function pager_array_splice($data, $limit = 10, $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);
}