How do I solve this CodeIgniter's 'Too few arguments to function' error?

Viewed 4973

Good day,everyone! Please, am new to CodeIgniter... Actually,am try to write a search module script using inner join, but I don't know what am not getting right.

This is the error message: An uncaught Exception was encountered Type: ArgumentCountError

Message: Too few arguments to function Career_model::quick_search(), 0 passed in C:\xampp\htdocs\hue\application\controllers\Pages.php on line 69 and exactly 4 expected

Filename: C:\xampp\htdocs\hue\application\models\Career_model.php

Line Number: 124

Backtrace:

File: C:\xampp\htdocs\hue\application\controllers\Pages.php Line: 69 Function: quick_search

File: C:\xampp\htdocs\hue\index.php Line: 286 Function: require_once

This is my controller:

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

And here is the Model:

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();

    }

Thanks in advance...

1 Answers

The error is because you're not providing any parameters when you call your model in else condition(which expects 4 parameters). I've written a possible solution for you, see if it helps.

Remove this code to the one below --

// 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;

to

// 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);
Related