1

I try to build new module (Drupal 7), in some page I show the results to the user as a table like the following:

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

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

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

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

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->start_date;
    while($timeInLoop < $row->end_date){
        //split the row to multi row (one week for each row)
        $timeInLoop = strtotime(date("Y-m-d", $timeInLoop) . " +1 week");
        $row->due_date = date("Y-m-d", $timeInLoop);
        $rows[] = array('data' => (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

1 Answer 1

0

I find the solution here

$limit = 10;
$rows = pager_array_splice($rows, $limit);
$form['tablesort_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => 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);
}

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.