CodeIgniter Forums
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. Smile


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...
$data['pagetitle'] = $this->config->item('item_name', 'item_value');
$this->load->view('news_view', $title);



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');

class Settings {

    private $CI;
    
    private $items = array();
    
    function __construct() {
        $this->CI =& get_instance();
        $this->CI->load->module_model('settings', 'settings_m');
    }
    
    function item($slug = '') {
        
        // Check for previously called settings and return it if found
        if(in_array($slug, array_keys($this->items)))
            {
            return $this->items[$slug];
            }
        
        // Check the database for this setting
        if ($setting = $this->CI->settings_m->getSetting($slug))
        {
            $value = $setting->value;
            }    
        
        // Not in the database? Try a config value instead
        else
        {
            $value = $this->CI->config->item($slug);
        }
        
        // Save the value in the items array and return it
        return $this->items[$slug] = $value;
    }
    
    function set_item($slug, $value) {
        return $this->CI->settings_m->updateSetting($slug, array('value'=>$value));
    }
    
    function formControl(&$setting) {
        switch($setting->type) {
            default:
            case 'text':
                $form_control = form_input(array('id'=>$setting->slug,
                                                'name'=>$setting->slug,
                                                'value'=>($setting->value)));
            break;
            
            case 'textarea':
                $form_control = form_textarea(array('id'=>$setting->slug,
                                                    'name'=>$setting->slug,
                                                    'value'=>($setting->value)));
            break;
            
            case 'select':
                
                $form_control = form_dropdown($setting->slug, $this->_formatOptions($setting->options), $setting->value);
            break;
            
            case 'checkbox':
            case 'radio':
                
                $func = $setting->type == 'checkbox' ? 'form_checkbox' : 'form_radio';
                
                $form_control = '';
                
                foreach($this->_formatOptions($setting->options) as $value => $label):

                    $form_control .= ' '.form_radio(array('id'=>$setting->slug,
                                                          'name'=>$setting->slug,
                                                          'checked'=>$setting->value == $value,
                                                          'value'=>$value))
                                    . ' '.$label;
                              
                endforeach;
            break;
            case 'function':
                $search_for_function = $setting->options;
                // Try to find the function named in the DB
                if (is_callable(array($this, $search_for_function)))
                {
                    $form_control = $this->$search_for_function($setting);
                } else {
                    $form_control = "Error: Function named ".$search_for_function." not found.";
                }
            break;
        }
        
        return $form_control;
    }
    
    function _formatOptions($options = array()) {
        $select_array = array();
        
        foreach(explode('|', $options) as $option):
            list($value, $name)=explode('=', $option);
            $select_array[$value] = $name;
        endforeach;
        
        return $select_array;
    }

}

?>

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?