朋友们,我有这个功能来显示所有文章,我一次又一次地为不同的类别编写这个函数,因为codeigniter参数与url有关我如何传递参数以便我可以重用这个函数?
这是我的控制器功能,可以显示所有新闻。
function all_news(){
//do some pagination
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/news/all_news';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 7;
//some css for pagination
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
//initialize pagination
$this->pagination->initialize($config);
//end pagination
$data['title'] =" All News";
//for pagination
$data['query']= $this->db->order_by('id','desc');
$data['query'] = $this->db->get('articles',$config['per_page'],$this->uri->segment(3));
$this->load->vars($data);
$this->load->view('main/all_news');
}
答案 0 :(得分:1)
考虑将大部分控制器逻辑转移到模型方法中。然后,您将能够向此方法发送参数,该参数将根据您发送给方法的参数将数据库结果返回给控制器。
答案 1 :(得分:0)
http://example.com/index.php/news/local/metro/crime_is_up
细分数字是这样的:
1-新闻
2,局部
3,地铁
4- crime_is_up
$product_id = $this->uri->segment(3);//metro
完整信息https://www.codeigniter.com/user_guide/libraries/uri.html