Codeigniter 从搜索查询中获取行数

发布于 2024-12-13 01:04:14 字数 1192 浏览 1 评论 0原文

我的控制器中有一个查询,用于搜索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-12-20 01:04:14

您可以在模型中添加一个返回计数的函数并调用它。

例子:

// model
public function getAffectedRows()
{
    return $this->db->affected_rows();
}

...

// controller
$this->Search_model->doQuery();
$numRows = $this->Search_model->getAffectedRows();

You can add a function in your model that returns the count and call that.

Example:

// model
public function getAffectedRows()
{
    return $this->db->affected_rows();
}

...

// controller
$this->Search_model->doQuery();
$numRows = $this->Search_model->getAffectedRows();
得不到的就毁灭 2024-12-20 01:04:14

您可以只使用

$viewdata['search_results'] = 
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);

$viewdata['search_result_count'] = count( $viewdata['search_results'] );

或者您可以从模型中返回一个包含计数的结构化数组。像这样的东西

return array( 'results' => $result, 'num_results' => $this->db->num_rows() );

You can either just use

$viewdata['search_results'] = 
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);

$viewdata['search_result_count'] = count( $viewdata['search_results'] );

Or you could return a structured array from your model which includes the count. Something like

return array( 'results' => $result, 'num_results' => $this->db->num_rows() );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文