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.