如何在codeigniter php控制器函数中传递参数

问题描述:

嗨,朋友我有这个函数来显示所有文章,我一次又一次地为不同的类别写这个函数,因为codeigniter参数与url有关,我如何传递参数使我可以重用这个功能吗?如何在codeigniter php控制器函数中传递参数

这是我的控制器功能来显示所有消息。

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'); 
} 

考虑将大部分控制器逻辑移动到模型方法中。然后,您将能够向此方法发送参数,该参数将根据您发送给方法的参数将数据库结果返回到控制器。

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