Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] learning how to pass data to a view

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:

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

function get_latest()
        $query = $this->db->get('movies', 10);
        return $query->result();

&lt;?php foreach($query as $movie):?&gt;

<li>&lt;?php echo $movie;?&gt;</li>

&lt;?php endforeach;?&gt;
//Output: Object of class stdClass could not be converted to string
&lt;?php echo $query['name']; ?&gt;
//Output: Undefined index: name
//so maybe it's an object?
&lt;?php echo $query->name; ?&gt;
//Output: Trying to get property of non-object
//it has to be array then
&lt;?php echo $query; ?&gt;
//Output: Array
//yep, it's array..var_dump!
&lt;?php var_dump($query); ?&gt;
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*

The result method gets you an array with as values objects so if you do
&lt;?php foreach($query as $movie):?&gt;

<li>&lt;?php echo $movie->name;?&gt;</li>

&lt;?php endforeach;?&gt;
You get the result you want.

That was fast!

Thanks xwero!

Now i get it. Array with object values *takes note*

* Edited, much better answer already


I have one more question!

Say i want to pass instead of many rows, just one.

That was the browse view, then i'm linking each item as follow:
<a href="index.php/movie/id_here">(...)</a>

So i click on the item and goes to show, show get's the data from db and etc
My problem is, again, passing the data to the view.

Since i have only one row, i don't need foreach, so then how to do?

I can still use foreach for only one item? Isn't there a way to use $query->name or something?


instead of result you have to use row and then you have a single object

Perfect! thanks again :cheese:

Theme © iAndrew 2016 - Forum software by © MyBB