Welcome Guest, Not a member yet? Register   Sign In
Sending content from Controllers to Views
#3

[eluser]talldarkmystere[/eluser]
The Model - /system/application/models/info_model.php
Firstly, we'll build a special model for outputting our info links. Essentially, we're loading our url helper to make our links in the constructor, then in our info_items function, we're building an array, just like you did, but we're using our url helper to handle the links instead of hardcoding them.

Code:
<?php
Class Info_model extends Model {
function Info_model()
{
  parent::Model();
  $this->load->helper('url');
}

function info_items()
{
$items = Array(
    anchor('link/url/here','link text'),
    'second info item is text',
    anchor('other/link/url','link 2 text')
  );
return $items;
}
}



The Controller - /system/application/controllers/test.php
In our controller, we're going to load the model we just made into the constructor, this makes the model available to all of our functions as $this->Info (see where we assigned it an alias in the second parameter? Next, we're asking for that array of info items from the model, and setting them as the contents of $data['info_items']. After that, we can load our view, and pass it the data array...

Code:
<?php
class Test extends Controller {

    function Test()
    {
      parent::Controller();
      $this->load->model('Info_model','Info');
    }

    function index()
    {
        $data['info_items'] = $this->Info->info_items();
        $this->load->view('my_view', $data);
    }

}


The View - /system/application/views/my_view.php
Now, we have our view - Which takes the $info_items array, and returns each item as a list item in our HTML code.

Code:
<ul>
&lt;?php foreach($info_items as $item)
{
print("<li>$item</li>\n");
}
?&gt;
</ul>



I hope that explains it well. Feel free to ask if you have any other questions!


Messages In This Thread
Sending content from Controllers to Views - by El Forum - 01-20-2011, 10:22 AM
Sending content from Controllers to Views - by El Forum - 01-20-2011, 08:16 PM
Sending content from Controllers to Views - by El Forum - 01-21-2011, 05:54 PM
Sending content from Controllers to Views - by El Forum - 01-21-2011, 07:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB