Php 这是什么原因造成的;缺少Categories::posts()的参数2;错误?
我正在开发一个小型博客应用程序。其后端和前端之间有明显的分离 后端是一个API,由Codeigniter 3制成,可以输出页面、帖子、分页等 在此API中,Php 这是什么原因造成的;缺少Categories::posts()的参数2;错误?,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在开发一个小型博客应用程序。其后端和前端之间有明显的分离 后端是一个API,由Codeigniter 3制成,可以输出页面、帖子、分页等 在此API中,\u initPagination()在Posts控制器中,在整个站点中使用: private function _initPagination($path, $totalRows, $query_string_segment = 'page') { //load and configure pagination $this->
\u initPagination()
在Posts控制器中,在整个站点中使用:
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;
$config['query_string_segment'] = $query_string_segment;
$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
我的错误在哪里?这意味着您向该方法传递了1个参数,而不是2个参数。发送它
$path
和$category\u id
或
如果你想说有时第二个参数可能不存在,那么重写如下:
public函数posts($path,$category_id=null){
在这种情况下,$category\u id
默认为null
或
public函数posts($path=null,$category\u id){
在这种情况下,
$path
将默认为null
。错误是否显示任何行号?它显示行号11,即:公共功能帖子($path,$category\u id)
您在哪里叫这个function@RazvanZamfir,这意味着您向该方法传递了1个参数,而不是2个。该漏洞不在函数posts()
中。它发生在调用post()的地方
。您没有提供$category\u id
参数。现在我在这一行上得到错误试图获取非对象的属性:$data['category\u name']=$this->Categories\u model->get\u category($category\u id)->name;
。这是因为$this->Categories\u model->get\u categority($categority\u id)
不返回对象。并且不能对其使用->name
语法。请检查此方法(方法名称为get\u category
,模型名称为Categories\u model
)。此外,这是一个新问题,最好在新主题中使用此方法的代码进行查找。我已回答了您的实际问题。您的实际问题已解决?您可以在单独的帖子中提出下一个问题,并提供解决该问题所需的信息。由于非对象错误是由$ca引起的,因此未回答tegory\u id
为null
$category\u id
必须具有非null值,或者$this->Categories\u model->get\u category($category\u id)
失败并返回false(而不是对象)。是的,因为我告诉过你,或者,请再看一次我的答案。$category=null
意味着有时$category
可能不存在。因此,如果它总是在这里->您不需要=null
。因此,您必须向此方法发送两个参数,$path和$category。并且您只传递了1个参数。检查您的位置使用此方法并检查变量,它应该类似于posts($pth,$cat_id)或类似内容。
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));
}
Missing argument 2 for Categories::posts()