简体   繁体   English

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

[英]how to pass arguments in codeigniter php controller functions

hi friends i have this function to show all articles, i am writing this function again and again for different categories, because codeigniter arguments are related to url how do i pass arguments so that i can reuse this function ? 嗨,朋友们,我有这个功能可以显示所有文章,我一次又一次地为不同的类别编写该功能,因为codeigniter参数与url有关,我如何传递参数以便可以重用此功能?

This is my controller function to show all news. 这是我的控制器功能,可显示所有新闻。

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

Consider moving much of your controller logic into a model method. 考虑将您的大部分控制器逻辑转移到模型方法中。 Then you'll be able send arguments to this method which will return back database results to your controller based on the arguments you send to the method. 然后,您将能够向该方法发送参数,这将根据您发送给该方法的参数将数据库结果返回给控制器。

http://example.com/index.php/news/local/metro/crime_is_up http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this: 段号是这样的:

1-news 1新闻
2-local 2本地
3-metro 3地铁
4-crime_is_up 4-crime_is_up

 $product_id = $this->uri->segment(3);//metro

full info https://www.codeigniter.com/user_guide/libraries/uri.html 完整信息https://www.codeigniter.com/user_guide/libraries/uri.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM