Codeigniter 从搜索查询中获取行数
我的控制器中有一个查询,用于搜索 url 中的术语/关键字。
示例 /search/keyword
然后通过我的控制器执行搜索:
$viewdata['search_results'] =
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);
然后我从模型中检查数据库,如下所示:
function search($searchquery, $limit, $offset) {
$this->db->from('content');
$this->db->like('title', $searchquery);
$this->db->or_like('content', $searchquery);
$query = $this->db->limit($limit, $offset)->get();
$results = Array();
foreach ($query->result() as $row) {
$results[] = Array(
'title' => '<a href="' . base_url() . $row->anchor_url . ' ">' . strip_tags($row->title) . '</a>',
'link' => base_url() . $row->anchor_url,
'text' => str_ireplace($searchquery, '<b>' . $searchquery . '</b>', strip_tags(neatest_trim($row->content,220,$searchquery,120,120)))
);
}
return $results;
}
}
我想知道如何返回在搜索中找到的行数结果到控制器。然后我将使用找到的行数进行分页。
如何将行数输入控制器???
I have a query in my controller which searches for the term/keyword that is in the url.
Example /search/keyword
Then through my controller I perform the search doing:
$viewdata['search_results'] =
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);
then I check the database from my model like so:
function search($searchquery, $limit, $offset) {
$this->db->from('content');
$this->db->like('title', $searchquery);
$this->db->or_like('content', $searchquery);
$query = $this->db->limit($limit, $offset)->get();
$results = Array();
foreach ($query->result() as $row) {
$results[] = Array(
'title' => '<a href="' . base_url() . $row->anchor_url . ' ">' . strip_tags($row->title) . '</a>',
'link' => base_url() . $row->anchor_url,
'text' => str_ireplace($searchquery, '<b>' . $searchquery . '</b>', strip_tags(neatest_trim($row->content,220,$searchquery,120,120)))
);
}
return $results;
}
}
I would like to know how I can return the number of rows found in my search result to the controller. I will then use the count of rows found for pagination.
How can I get the row count into the controller????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在模型中添加一个返回计数的函数并调用它。
例子:
You can add a function in your model that returns the count and call that.
Example:
您可以只使用
或者您可以从模型中返回一个包含计数的结构化数组。像这样的东西
You can either just use
Or you could return a structured array from your model which includes the count. Something like