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

[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>&lt;?=$header;?&gt;</h2>
<ul>
&lt;?php foreach($query as $movie):?&gt;

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

&lt;?php endforeach;?&gt;
</ul>
//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;
//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*
#2

[eluser]xwero[/eluser]
The result method gets you an array with as values objects so if you do
Code:
<h2>&lt;?=$header;?&gt;</h2>
<ul>
&lt;?php foreach($query as $movie):?&gt;

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

&lt;?php endforeach;?&gt;
</ul>
You get the result you want.
#3

[eluser]M4rc0[/eluser]
That was fast!

Thanks xwero!

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

[eluser]meigwilym[/eluser]
* Edited, much better answer already

Mei
#5

[eluser]M4rc0[/eluser]
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:
Code:
<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?

Thanks!
#6

[eluser]xwero[/eluser]
instead of result you have to use row and then you have a single object
#7

[eluser]M4rc0[/eluser]
Perfect! thanks again :cheese:




Theme © iAndrew 2016 - Forum software by © MyBB