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

发布于 2024-11-08 23:50:56 字数 984 浏览 0 评论 0原文

嗨,朋友们,我有这个函数来显示所有文章,我正在为不同的类别一次又一次地编写这个函数,因为 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');
}

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 ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

打小就很酷 2024-11-15 23:50:56

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

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.

苍白女子 2024-11-15 23:50:56

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

分段编号如下:

1-news
2-本地
3-地铁
4-crime_is_up

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

完整信息https://www.codeigniter.com/user_guide/库/uri.html

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

The segment numbers would be this:

1-news
2-local
3-metro
4-crime_is_up

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

full info https://www.codeigniter.com/user_guide/libraries/uri.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文