3

I have a problem quite similar to the one found here, where I am trying to form a table using the tutorial found on Ellis Lab's site. I found some of their guide totally lacking on pagination, and as a result when I implement it, I lose my table sorting and have links for pagination that do not work when clicked.

After looking on StackExchange I found the helpful post above, so that now I am at the same starting state as the OP's problem. My code looks like this:

$method_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=my_module'.AMP.'method=my_method';    
return array(
        'rows' => array_slice($rows, $offset, 5),
        'pagination' => array(
        'page_query_string'     => TRUE,
        'base_url'      => $method_url,
        'per_page'          => 5,
        'total_rows'        => count($rows)
        )
    );

However, adding $this->EE->table->set_base_url() does not solve it for me. Although I may need to add it in a place that I did not try - the solution post did not specify.

Perhaps the best way I could summarize would be this: What is the EllisLab tutorial missing from making a functional pagination/sorting table?

1

I used set_base_url() in my index() function, where I set up the table.

$this->EE->load->library('table');
$this->EE->table->set_columns(array(
// columns
));
$this->EE->table->set_base_url('C=addons_modules&M=show_module_cp&module=my_module');
$data = $this->EE->table->datasource('_datasource');
$data['base_url'] = $this->base_url; // Available in my View
return $this->EE->load->view('mcp_my_module', $data, TRUE);

And in my _datasource function, which needs to give the pagination array a base_url:

$base_url = $this->_full_url();

function _full_url()
{
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
    $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);

    $non_offset_uri = explode('&tbl_offset=',$_SERVER['REQUEST_URI']);
    $non_offset = $non_offset_uri[0];

    return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $non_offset;
}

Yes, I wrote my own function to set the base_url for pagination.

Also - I have this as the return for my _datasource function:

return array(
    'rows' => $rows,
    'pagination' => array(
        'page_query_string' => TRUE,
        'base_url'    => $base_url,
        'per_page'   => $per_page,
        'total_rows' => $total_rows
    )
);

I start it with this:

function _datasource($state)
{
    //var_dump($state['offset']);
    $offset = 0;
    if ($state['offset'] != 0)
        $offset = (int)$state['offset'];
    $per_page = 20;
    $base_url = $this->_full_url();

And use those values in my query:

LIMIT ?,?",array($offset,$per_page));

To get the current page records.

Note that I have TWO queries in my _datasource function: One returns all rows to get the $total_rows value. The other uses LIMIT to get the current page's records.

$count_results = $this->EE->db->query("SELECT * FROM exp_audit_log ORDER BY timestamp desc");
$total_rows = $count_results->num_rows();
28
  • I added limit via active record, and the page does not render pagination when it is included. The query is like this: $this->EE->db->select('field1, field2') ->from('table') ->limit('5', $offset) ->get() ->result_array();
    – Jord
    Mar 19 '13 at 19:42
  • Are you using a View? Or just an index() function? What if your index() function just returns $data['table_html'] . $data['pagination_html'];? Mar 19 '13 at 19:55
  • Did you set $offset = $state['offset']? Are you sure you have more than 5 rows? Mar 19 '13 at 19:57
  • I am using a view, not index() and $offset = $state['offset']. The query has 20 rows.
    – Jord
    Mar 19 '13 at 20:02
  • And your view has these? <?= $table_html ?> <?= $pagination_html ?> Mar 19 '13 at 20:10

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.