CodeIgniter Forums
Help! How to set some general (global) vaiables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help! How to set some general (global) vaiables? (/thread-32892.html)

Pages: 1 2


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]victorche[/eluser]
It's been a while, since I asked almost the same question and I still can not find the solution. For example, I have several controllers like "blog", "search", "faq".
But I have header everywhere (in every single page). And in this header, I have a navigation menu, with links, generated from a db result.
The header view looks like this:
Code:
<div id="menu">
    <ul>
        {top_menu}
        <li><a href="/{link}">{link}</a></li>
        {/top_menu}
    </ul>
</div>
For every controller I have a model like "blog_model", "search_model", "faq_model" (mostly for any kind of db operations). But because the header is some kind of general (common) stuff, I created a general_model.php for use with these common site parts. Not sure if this is the right approach anyway ... And there I have a small function get_links() which takes all the menu links from a db table.

So in my MY_Controller.php I am loading this general model like this:
Code:
$this->load->model('general_model', 'general');
In every controller I have pieces of code like this:
Code:
$links = $this->general->get_links();

$data = array(
    'top_menu' => $links,
    // other data here ...
);
So my question is:
Can I avoid this duplication of code? Imagine the blog controller... I have there function index (for the main blog view), function categories (to display posts from custom category), function view (to view a single post with the comments). And everytime I have to use $links = $this->general ... and then in the $data array ... 'top_menu' => ...
I tried at least to deal with the $links variable. I put this line in MY_Controller.php like:
Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();

        $this->load->model('general_model', 'general'); // I think there is no other way of loading models globally? Can this be removed too?

        $links = $this->general->get_links(); // The result is udefined variable: links



Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]gyo[/eluser]
This issue has been discussed a lot in the forum, next time search before.

I guess you need to load a view in a view:

Code:
$data['header'] = $this->load->view('header', '', true);

$this->load->view('main_template', $data);

This way the 'main_template' will be sent to the browser, and the variable '$header' inside it will be replaced with the 'header' view.
Note that the 'header' has been loaded as data with the 'true' paramenter, so that it's ready to be embedded in another view.

FYI: http://ellislab.com/codeigniter/user-guide/general/views.html


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]victorche[/eluser]
Thanks a lot, but this is not my problem. I am using Phil's template library and in my case I am asking for a variable, which I want to set in MY_Controller.php. And believe me, I was searching the forum for 2 weeks already.
That's why I am creating something like a general (common) model, which functions can be used globally, on every page. And I am loading this model (general_model.php) in MY_Controller.php
I am not sure if this is the right approach, but this way I can use all the common functions from general_model.php all over the site (in different controllers).
But I want something else. I want to declare a variable in one file and to use it in different controllers. Just because, as in this example with the menu links, I have these links on every page.
So instead of using this in every controller:
Code:
$links = $this->general->get_links();
I want to do it once, in MY_Controller.php
But it is not working. The general model is loaded, but declaring the variable $links there is not enough.


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]gyo[/eluser]
Alright, sorry I didn't get your problem. Smile

Just a quick thought... what about this?
Code:
$this->links = $this->general->get_links();

When you set $this->links inside MY_Controller, it will be available for all the controllers that extend it, the same way: $this->links.


Hope it helps!


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]victorche[/eluser]
[quote author="gyo / suashi" date="1281364019"]Alright, sorry I didn't get your problem. Smile

Just a quick thought... what about this?
Code:
$this->links = $this->general->get_links();

When you set $this->links inside MY_Controller, it will be available for all the controllers that extend it, the same way: $this->links.


Hope it helps![/quote]
Great ... but how should I call them in controllers?
Code:
// Like this:
$data = array(
    'top_menu' => $links,
    // ...
);
// Or like this:
$data = array(
    'top_menu' => $this->links,
    // ...
);



Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]gyo[/eluser]
Like this:

Code:
$data = array(
    'top_menu' => $this->links,
    // ...
);

You might need to set the variable in MY_Controller as public if you're using PHP5.


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]victorche[/eluser]
[quote author="gyo / suashi" date="1281367143"]Like this:
...
You might need to set the variable in MY_Controller as public if you're using PHP5.[/quote]
Really thanks :]
But another reply means another question :]
How can I set the variable as public? I know I can set a function as public, but variable...
Code:
public function this_is_public()
{
   // This is a public one ...
}
But how can I do it with a variable? Sorry for these stupid questions, but I am still learning, you know :/


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]John_Betong[/eluser]
I use numerous Controllers with a common MY_Controller.

Each individual controller only loads specific view $data and then the $data is passed through to a lengthy common method MY_Controller->view($data). This common view method then tests and loads other information before a final call to a single common view.
&nbsp;
&nbsp;
&nbsp;


Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]John_Betong[/eluser]
duplicate of my previous post which I am not authorised to delete.
Quote:Really thanks :]
But another reply means another question :]
How can I set the variable as public? I know I can set a function as public, but variable…

Set your public variable in MY_Controller
Code:
class MY_Jokes extends Controller
{
  public $atts    = array(
                    'width'      => '800',
                    'height'     => '600',
                    'scrollbars' => 'yes',
                    'status'     => 'yes',
                    'resizable'  => 'yes',
                    'screenx'    => '300',
                    'screeny'    => '100',
                    'title'      => 'email a friend'
                  );
        public $advert_top;
        public $advert_bottom;
        public $adsence_blocking, $advert_tower, $analytics;
        public $CACHED = TRUE; // default and toggle if a search



Help! How to set some general (global) vaiables? - El Forum - 08-09-2010

[eluser]victorche[/eluser]
@John_Betong, thanks ... It means I can do something like:
Code:
public $this->links = $this->general->get_links();
Does not look so right to me :/ Is it correct?