Welcome Guest, Not a member yet? Register   Sign In
Problem with a function with parameters
#1

[eluser]Unknown[/eluser]
Has this issue ever been discussed?

From my Controller class, I tried to call a function in a model with two parameters.

Model:
Code:
function some_function($a, $b)
{
$this->db->select('*');
$this->db->from('a_table');
return $this->db->get();
}

The problem is if I do something like this in my Controller class:

Controller:
Code:
$this->load->model('My_model');
if ($this->My_model->some_function($param1, $param2)->num_rows() > 0)
{
  echo "data found.";
}

it will return this error: "Fatal error: Call to a member function num_rows() on a non-object".

It will be different if I make a variable to call the function first:

Controller:
Code:
$this->load->model('My_model');
$query = $this->My_model->some_function($param1, $param2);
if ($query->num_rows() > 0)
{
echo "data found.";
}

This should be no error.
So, I have to make a variabel $query to represents the $this->My_model->some_function($param1, $param2) first, then I can use num_rows() on my Controller class.

Is there anybody ever got the same case?
#2

[eluser]InsiteFX[/eluser]
Of course not, it doe's not know how many rows there are until after it calls the method!
#3

[eluser]Aken[/eluser]
That should work fine. Your query probably failed, which means your model's method is returning false.




Theme © iAndrew 2016 - Forum software by © MyBB