Using codeigniters mvc, I have created a model for my search query and I would like to return it as an array.
I am a bit lost, I have created a foreach statement so that I can perform a function on each of the strings. (Not sure if it is the right way to go, and would now like to pass it as an array to my $data variable.
Could somebody point me in the right direction and highlight what I have done wrong:
<?php
class Search_model extends CI_Model {
function search($searchquery)
{
function supertruncate($text, $searchquery, $characters_before, $characters_after){
$pos = strpos($text, $searchquery);
$start = $characters_before < $pos ? $pos - $characters_before : 0;
$len = $pos + strlen($word) + $characters_after - $start;
$text = substr($text, $start, $len);
$last = strrpos($text, ' ');
return str_ireplace($word, '<span class="highlight" style="background: #E6E6E6;">' . $word . '</span>', $text);
}
$this->db->from('content');
$this->db->like('title', $data);
$this->db->or_like('content', $data);
$query = $this->db->get();
$characters_before="120";
$characters_after="120";
foreach ($query ->result() as $row)
{
$title = strip_tags($row->title);
$text = supertruncate(strip_tags($row->content), $searchquery, $characters_before, $characters_after);
}
return $data;
}
}