CodeIgniter Forums
Sending content from Controllers to Views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sending content from Controllers to Views (/showthread.php?tid=37784)



Sending content from Controllers to Views - El Forum - 01-20-2011

[eluser]Alexisu[/eluser]
Hello,

I'm sending a link from my Controller to my View page, as follows:

my controller....

Code:
<?php

class Test extends Controller {

    function index()
    {
        $data['info'] = array('<a href="/folder/something/">Link Title</a>', 'Text');
        $this->load->view('my_view', $data);
    }

}
?&gt;

my_view page....

Code:
<ul>
     &lt;?php foreach($info as $item): ?&gt;
         <li>&lt;?=$item?&gt;</li>
     &lt;?php endforeach; ?&gt;
</ul>


I'm just getting started with PHP & Codeigniter & I'm sending a lot of data such as this straight to my views from the controller. Furthermore, from my views, I load paragraph text from other nested views.

My question is: Is the above normal practice, or would the 'Model' ideally be involved, or a 'Helper' be used (perhaps for the link)? I'm trying to make sure my content/data is where it should be.

Any tips/advice would be appreciated!

Thanks.

Lex


Sending content from Controllers to Views - El Forum - 01-20-2011

[eluser]sooner[/eluser]
if your content has only html..then put those content directly in the views...if you have to do some logic..then do it in controller..if you need to access the database and get details do it in the model...


Sending content from Controllers to Views - El Forum - 01-21-2011

[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:
&lt;?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:
&lt;?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!


Sending content from Controllers to Views - El Forum - 01-21-2011

[eluser]Alexisu[/eluser]
Thank you!!

That is insanely useful! I'm going to digest that tomorrow & will let you know my thoughts.

Many thanks.