0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.