• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What causes this “Missing argument 2 for Categories::posts()” error?

#1
I am working on a small blogging application. There is a clear separation between its back-end and its front-end.
The back-end is an [b]API[/b], made with Codeigniter 3, that spits out pages, posts, pagination etc.


In this API, the [b]Categories[/b] controller displays, among others, [i]post by category[/i]:



Code:
public function posts($path, $category_id) {
    //load and configure pagination
    $this->load->library('pagination');
    $config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;
    $config['base_url'] = base_url('/categories/posts/' . $category_id);
    $config['query_string_segment'] = 'page';
    $config['total_rows'] = $this->Posts_model->get_num_rows_by_category($category_id);
    $config['per_page'] = 12;

    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }

    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);

    $data = $this->Static_model->get_static_data();
    $data['pagination'] = $this->pagination->create_links();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['category_name'] = $this->Categories_model->get_category($category_id)->name;
    $data['posts'] = $this->Posts_model->get_posts_by_category($category_id, $limit, $offset);

    // All posts in a CATEGORY
    $this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}

The code above has a bug who's cause I can not find. The error message the application throws is:

Code:
Missing argument 2 for Categories::posts()

Where is my mistake?
Reply

#2
it's telling you that it is not getting the $category_id

If you are setting it then just initialize it like so.


PHP Code:
public function posts($path$category_id 0)
{
    // your code...


Otherwise you are getting an empty $category_id

Do a var_dump on it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
The problem is not in Categories::posts()

The problem is how you are calling it. Show us the code where you call posts.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.