[eluser]M4rc0[/eluser]
Hi all!
I believe code igniter user guide is not very clear of how to pass data to a view.
I am doing exactly what it says, although in the user guide example it's passing static data do the view and not dynamic, i think it should work anyway:
controller
Code:
function latest()
{ //loading the model is in the constructor
//set option menu as current
$this->template->write('opt1', 'current');
//get latest added movies
$data['header'] = 'Latest Movies';
$data['query'] = $this->Movie_model->get_latest();
// Write the browse view in the template
$this->template->write_view('content', 'movie/browse', $data, TRUE);
// Render the template
$this->template->render();
}
model
Code:
function get_latest()
{
$query = $this->db->get('movies', 10);
return $query->result();
}
view
Code:
<h2><?=$header;?></h2>
<ul>
<?php foreach($query as $movie):?>
<li><?php echo $movie;?></li>
<?php endforeach;?>
</ul>
//Output: Object of class stdClass could not be converted to string
<?php echo $query['name']; ?>
//Output: Undefined index: name
//so maybe it's an object?
<?php echo $query->name; ?>
//Output: Trying to get property of non-object
//it has to be array then
<?php echo $query; ?>
//Output: Array
//yep, it's array..var_dump!
<?php var_dump($query); ?>
//Output:
array(1) { [0]=> object(stdClass)#17 (17) { ["id"]=> string(1) "1" ["name"]=> string(5) "Teste" ["cover"]=> string(32) "public/images/covers/nocover.jpg" ["cover_thumb"]=> string(38) "public/images/covers/nocover_thumb.jpg" ["watched"]=> string(1) "0" ["rate"]=> string(1) "0" ["country"]=> string(0) "" ["year"]=> string(1) "0" ["comment"]=> string(6) "blabla" ["imdb_link"]=> string(0) "" ["trailer_code"]=> string(0) "" ["director"]=> string(0) "" ["actors"]=> string(0) "" ["comp_link"]=> string(0) "" } }
You see i'm trying many ways to show the data to find out how but none of them work :down:
Help! Something so simple it should be explained on user guide *frustraded*