![]() |
Can I create global variables? - 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: Can I create global variables? (/showthread.php?tid=15088) |
Can I create global variables? - El Forum - 01-26-2009 [eluser]Robert May[/eluser] Not sure how I'd do this in CI. I basically want to pass the page title to the view via a variable, ideally without having to pass it through the load->view function. Or can I pass multiple variables via that function, ie. Code: $this->load->view('news_view', $data, $pagetitle); Any help is much appreciated. ![]() Can I create global variables? - El Forum - 01-26-2009 [eluser]walrus_lt[/eluser] The aswer is Code: $this->config->set_item('item_name', 'item_value'); And now `item_name` will be writed to config list (temporarily) And you can use it everywhere... And wtf you are doing with third parameter? You must add $pagetitle to you $data array Code: // Add pagetitle to... Can I create global variables? - El Forum - 01-26-2009 [eluser]Robert May[/eluser] Ooh, excellent. Thanks very much. On a similar line of thought, is there a way to load config data from a database? I want to load user preferences globally, but have yet to figure out how to do it! Thanks again! Can I create global variables? - El Forum - 01-26-2009 [eluser]Phil Sturgeon[/eluser] This is still PHP so you can still use global variables as you would in any other PHP app, but config items are a good way to go. Remember though, that $_GLOBAL would work even in libraries and helpers where there is no CI instance by default. [quote author="Robert May" date="1232998445"]Ooh, excellent. Thanks very much. On a similar line of thought, is there a way to load config data from a database? I want to load user preferences globally, but have yet to figure out how to do it![/quote] You could extend the config class but I'm not a fan of that. I made a settings class and model, but you could do it with just a model for a more simple version. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Make a model for this too ofc to handle the insert/select. Then you can autoload it and use: Code: $this->setting->item('something'); I like to keep config and settings different as they are different things, but this will also default to config values if it can't be found in the db. Just for a bit of flexibility. Can I create global variables? - El Forum - 07-01-2009 [eluser]Vangelis B[/eluser] I'm also looking for a "global variables" solution mainly for passing some standard data to the views easily (like default image path blah blah). I ended up creating classes in helpers. Then I create new objects once the helper is run. So I can do $path->images in a view to get the images path. I would welcome a more elegant solution. Can I create global variables? - El Forum - 07-01-2009 [eluser]jedd[/eluser] Can you explain why a config setting is inappropriate for a default image path? |