1

I am creating a plugin in which I select data from a table from other database, outside EE. Now to display the result in EE template, I have to traverse through records and create HTML within the plugin and assign that to a variable, that I return to the Template.

Is there a way to simply return the recordset to the template, and then use template to show the records, and also show the pagination? Pagination is a big issue for such cases.

1
  • Anyone please..? – Deep Jun 3 '13 at 7:56
1

Instead of creating the HTML in the plugin, you should use the Template Class for outputting the database results.

Example for Creating Pagination:

$total = 300;//Set from your recordset total
$per_page = 30;//how many records you want to show per page
$base_url = $this->EE->config->_global_vars['base_url'].'/'.$this->EE->uri->segment(1);//The url for the listing that EE will use for the pagination links

$this->EE->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['num_links'] = 3;
$config['uri_segment'] = 4;
$config['first_link'] = '1';
$config['first_tag_open'] = '<div class="pag-first">';
$config['first_tag_close'] = '</div>';
$config['next_tag_open'] = '';
$config['next_tag_close'] = '';
$config['num_tag_open'] = '';
$config['cur_tag_open'] = '<strong>';
$config['prev_tag_open'] = '';
$config['last_link'] = ceil($total/$per_page);
$config['last_tag_open'] = '<div class="pag-last">';
$config['last_tag_close'] = '</div>';

$this->EE->pagination->initialize($config);

$myPagination = $this->EE->pagination->create_links();

http://ellislab.com/expressionengine/user-guide/development/module_tutorial.html About halfway down the page shows an example.

Assuming you haven't already sorted the listing itself (from the recordset), you want a tag pair in your plugin utilising $this->EE->TMPL->tagdata. You need an array of the data, the following example will contain stuff you don't need!

In this example I've got a response from an external database returned as an object called "Products":

$total = $products->hits->total;
if (empty($total) || empty($products->hits->hits))
    return;

$productOutput = array();
foreach($products->hits->hits as $key => $product) {
    $productDetails = array(
        "id"                => $product->_id,
        "name"              => ucfirst(stripslashes(strtolower($this->truncate($product->_source->name, $name_truncation)))),
        "url"               => $product->_source->friendly_url,
        "price"             => $product->_source->price,
        "original_price"    => $product->_source->original_price
    );
    $productOutput[] = $productDetails;
} //###   end of foreach   ###

$outputVars["pagination"] = $myPagination;
$outputVars["products"] = $productOutput;
$output = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, array($outputVars));

Note: You will have to restrict the data set to what you want to appear. So I'd suggest you do 2 queries to your external Database - one to get the total results only, the other to get a portion of the results that you need to display (to keep query speed down). To get the current page (basically calculate the offset you can get the relevant segment, e.g. $this->EE->uri->segment(5, 0);

The above code will loop through your plugin once (note the array($outputVars)). Within your plugin tags, you'll need {pagination} tag to output the pagination wherever you want in the template. And you'll need the {products} ... {/products} tag pairs to output the data recordset - which will loop for the amount you want shown.

4
  • Peter, I'm sorry to say this, but a lot of these variables don't exist anymore. Take a look at system/expressionengine/libraries/EE_pagination.php. – Robson Sobral Jun 3 '13 at 12:46
  • This code is being used on EE 2.5.2 and all those variables are still referenced in the EE documentation. I've checked the EE changelogs and there's no reference to changes there since 2.5.2. The CodeIgniter library: ellislab.com/codeigniter/user-guide/libraries/pagination.html. Also note, that I've included code as is and I doubt that all those variables will be even required for this instance. – Peter Lewis Jun 3 '13 at 16:42
  • Please, forgive me, Peter. I was wrong. These variables still exists, but EECMS uses its own Pagination Class for templates since version 2.4: ellislab.com/expressionengine/user-guide/about/… . I saw the docs, too. The CodeIgniter Pagination Class is used just on CP. – Robson Sobral Jun 3 '13 at 17:21
  • No worries - I've only referenced the CodeIgniter documentation as they're far more detailed than the EE docs :) And I was trying to search for the super powers example for template parsing, which you've added in your edit - thanks. – Peter Lewis Jun 3 '13 at 17:46

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.