• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with controller and accessing object

#1
[eluser]Robert M.[/eluser]
Hi @ all together.

This will be my first time here in the CI Forum. A have a small problem with understading how i can access an result in controller.

I got an error message like \"Trying to get property of non-object\" on line...

I want to do some like this.


Code:
function get_something($some_var){

    $this->load->model(\'some_model\',\'\',TRUE);
    $data[\'query\'] = $this->some_model->get_some_thing($some_var);
    
    if($data[\'query\']->col2==some_value){    //<--this will be the line where i get the error.
        $this->load->view(\'view_1\',$data);
    }else{
        $this->load->view(\'view_2\',$data);
    }
    

}

i know my mistake but i don\'t understand how i can access the value. normally in the view i do some like this

Code:
foreach($query as $row):
echo \"col2: \".$row->col2;

and so on but i want access the vaule in the controller

the function in some_model looks like this...

Code:
function get_some_thing($some_var){

    $this->db->select(\'col1\');
    $this->db->select(\'col2\');
    $this->db->select(\'col3\');
    $this->db->where(\'col1\',$some_var);
    
    $data = $this->db->get(\'some_table\');
    return $data->result();
}

#2
[eluser]Phil Sturgeon[/eluser]
Sorry, what? You want to access $query in the controller right? Then use $data[\'query\']...

Anyway, you arent able to do it like this. The code should be:

Code:
function get_some_thing($some_var){

    $this->db->select(\'col1\');
    $this->db->select(\'col2\');
    $this->db->select(\'col3\');
    $this->db->where(\'col1\',$some_var);
    
    $data = $this->db->get(\'some_table\');
    return $data->row();
}

Notice how i switched result() to row(). That should sort it out.

#3
[eluser]Robert M.[/eluser]
So OK. I change it to row()
Now it jump into the if and load the view
Code:
if($data[\'query\']->col2==some_value){
$this->load->view(\'view_1\',$data);
}

in the view i try to access the values but i have some trouble with this. When i dump them i get this.
Code:
array(3) {
  [\"view\"]=>
  string(6) \"view_1\"
  [\"vars\"]=>
  array(3) {
    [\"col_1\"]=>
    string(1) \"1\"
    [\"col_2\"]=>
    string(5) \"test1\"
    [\"col_3\"]=>
    string(5) \"test2\"
  }
  [\"return\"]=>
  bool(false)
}
Now it is an array and not an object. The if statement in the conntroller works on the object but not in the view. What i make wrong.

I want to do something like this in the view.
Code:
echo $data->col1;

THX for help Wink

#4
[eluser]Robert M.[/eluser]
problem solved thx to longwave


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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