The algorithm implemented in PHP's similar_text()
function is not available in MySQL. There is soundex in mysql that could be used with an expression like:
... where name sounds like "some input"
in sql, so in your PHP code this would look like:
$v = new Vendedor();
$v->where('CONCAT(name, " ", lasta_name1, " ", lasta_name2) sounds like ', $name);
// ...
but soundex is doesn't really work with non english text. You can try various implementation of levenshtein too, see this or this.
Both of these will still have pretty bad performance since the database still have to compare every row so you don't really win much on that front but you change the algorithm of similarity and that might not be ok.
If you decide to keep the similar_text()
function you can implement the pagination on the array with an array_slice()
preferably only creating the order once and saving the information in some cache (memcached, plain files, apc cache...) so the subsequent calls with this input can be served without recalculating the order thus being faster. I imagine something like this (the cache parts are optiona):
function get_similar_vevendors($name, $offset, $limit) {
// Imaginary cache library,
// it would return the previously cached search results under a key
// created from the name
if (Cache::has('similar_vevendors_'.$name)) {
$ven = Cache::get('similar_vevendors_'.$name)
} else {
// your orignal code here, filling $ven array filled with similar Vendedor's
// ...
// saving the created similar vevendors array in cache
Cache::store('similar_vevendors_'.$name, $ven);
}
// return both the count of the results for pagination
// and a acting as the current "page"
return array(count($ven), array_slice($ven, $offset, $limit));
}
This way you can use the same parameters for paging trough the array that you would use in SQL and you can init CI's pagination library with something like this:
// $offset is coming from the links generated by the pager
list($total_rows, $paged_vens) = get_similar_vevendors($name, $offset, $items_per_page);
$this->pagination->initialize(array(
'per_page' => $items_per_page,
'total_rows' => $total_rows,
));