1

我的控制器中有一个查询,用于搜索 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;
    }

}

我想知道如何将在搜索结果中找到的行数返回给控制器。然后,我将使用找到的行数进行分页。

如何将行数输入控制器????

4

2 回答 2

1

您可以只使用

$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() );
于 2011-11-01T15:38:28.130 回答
1

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

例子:

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

...

// controller
$this->Search_model->doQuery();
$numRows = $this->Search_model->getAffectedRows();
于 2011-11-01T15:36:08.400 回答