如何解决此CodeIgniter的“函数实参过多”错误?

时间:2020-05-15 17:20:23

标签: php codeigniter

大家好! 请,我是CodeIgniter的新手...实际上,我尝试使用内部联接编写搜索模块脚本,但我不知道有什么不对劲。

这是错误消息: 遇到未捕获的异常 类型:ArgumentCountError

消息:函数Career_model :: quick_search()的参数太少,第69行的C:\ xampp \ htdocs \ hue \ application \ controllers \ Pages.php中传递了0,并且恰好是4

文件名:C:\ xampp \ htdocs \ hue \ application \ models \ Career_model.php

行号:124

回溯:

文件:C:\ xampp \ htdocs \ hue \ application \ controllers \ Pages.php 行:69 功能:quick_search

文件:C:\ xampp \ htdocs \ hue \ index.php 线:286 功能:require_once

这是我的控制者:

function quick()
{
    if ($this->session->userdata('username')):
        $session_data = $this->session->userdata('username');

        $query = $this->db->query("select * from account where email='$session_data'");
        $data = $query->result();

        foreach ($data as $key => $value);

        $this->form_validation->set_rules('gender','Gender', 'required');
        $this->form_validation->set_rules('from','lowest age', 'required');
        $this->form_validation->set_rules('to','highest age', 'required');

        if ($this->form_validation->run() == FALSE):
            $this->load->view('pages/search_result');
        else:
            $gender = $this->input->get_post('gender');
            $from = $this->input->get_post('from');
            $to = $this->input->get_post('to');
            $country = $value->country;

                //I suppose this is where the problem is coming from

            if($this->Career_model->quick_search($gender,$from,$to,$country)):
                $data = $this->session->set_flashdata('error','something went wrong...');

                redirect('pages/search_result',$data);
            else:
                $result['data'] = $this->Career_model->quick_search();
                redirect('pages/search_result',$result);
            endif;
        endif;
    endif;
}

这是模型:

function quick_search($gender,$from,$to,$country)
    {

        $this->db->join("about" , "account.ref_id = about.user_id");
        $this->db->where('gender',$gender);
        $this->db->where('country',$country);
        $this->db->where('age',$from);
        $this->db->where('age <=',$to);
        $query = $this->db->get('account');

        return $query->result();

    }

预先感谢...

1 个答案:

答案 0 :(得分:0)

错误是因为在其他条件(需要4个参数)下调用模型时,您没有提供任何参数。我为您写了一个可能的解决方案,看看是否有帮助。

将此代码删除为以下代码-

// Remove this code
if($this->Career_model->quick_search($gender,$from,$to,$country)):
    $data = $this->session->set_flashdata('error','something went wrong...');
    redirect('pages/search_result',$data);
else:
    $result['data'] = $this->Career_model->quick_search(); // this line is causing error because 0 parameters are passed.
    redirect('pages/search_result',$result);
endif;

// This is probably what you want ↓↓ 
   $result = $this->Career_model->quick_search($gender, $from, $to, $country); // get the data once and then check if data empty or not

if(!empty($result)){ // success

    $data['result'] = $result;

}else{  // fail

    $this->session->set_flashdata('error','No data found');
}
redirect('pages/search_result',$data);