Welcome Guest, Not a member yet? Register   Sign In
selecting data from database
#1

[eluser]brian88[/eluser]
in my controller I want to echo out the id for my jokes. print_r does return an id in the array.

and inside the view, I can echo it out fine. but it wont echo out in the controller. I get "Trying to get property of non-object"

Code:
public function category($catId = null)
{

$data = array(
     // models
     'genNav' => $this->main_mod->getGenNav(),
     'topicNav' => $this->main_mod->getTopicNav(),
     'jokes' => $this->main_mod->getJokesByCat($catId) // using this
    );
    
    foreach($data['jokes'] as $joke){
     echo $joke->id; // this works, but i dont want a loop
    }
    
echo $data['jokes']->id; // y does this not work?
echo $data['jokes']['id']; // or this doesnt work either
  
    if ( !empty($data['jokes']) ){
  $this->load->view('main/cat_header_view.php', $data);
  $this->load->view('nav_view.php', $data);
  $this->load->view('sort_view.php');
  $this->load->view('category_view.php', $data);
  $this->load->view('main/footer_view.php');
    }else{
     $this->load->view('main/cat_header_view.php', $data);
  $this->load->view('nav_view.php', $data);
  $this->load->view('sort_view.php');
  $this->load->view('category_no_jokes_view.php', $data);
  $this->load->view('main/footer_view.php');
    }
}
#2

[eluser]Denzel[/eluser]
Because data['jokes'] is an array, hence why it works with foreach. I'm going to take a wild guess that your secondary index is an integer. This should work:
Code:
$data['jokes'][0]->id
If an integer is not your secondary index, then figure out what is with a var_dump and use that.
#3

[eluser]brian88[/eluser]
ahh. I need to return 1 object and not an array of objects in the model. im dumb, thanks!




Theme © iAndrew 2016 - Forum software by © MyBB