Welcome Guest, Not a member yet? Register   Sign In
display common data accross all pages
#11

[eluser]InsiteFX[/eluser]
Code:
$this->load->vars($this->data);

// or
$this->load->vars($data);

Puts all the $data variables into CodeIgniters global cachced variables array. Which means they are avaiable anywhere in your application.
Then you do not need to pass them to your views.

See: the CodeIgniter Loader Class.

InsiteFX
#12

[eluser]John_Betong_002[/eluser]
@InsiteFX

I have read the User Guide many times on the Loader Class and unfortunately do not appreciate the implications.

// MY_Controller -> __construct -> $this->today
I get the impression that setting the above variable defaults to a public class variable and therefore $this->today is available in all controllers and views (but not models).

// $data['today'] = array('1','2','3');
// MY_Controller -> __construct -> $this->load->vars($data);
I just tried the above and $today was not available in a sub-controllers but was available in my view?

I think I will stick with setting a MY_Controller -> Public $variable.
 
 
#13

[eluser]jmadsen[/eluser]
"this function produces the same result as using the second parameter of the $this->load->view() function...if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. "

from the core Loader class comments itself,

"Once variables are set they become available within the controller class and its "view" files."


I'm also curious to dig deeper into the code behind it - I confirmed that only the loading controller seems to know about it, not any sub-controllers. that would really limit its usefulness, as far as I can tell.
#14

[eluser]John_Betong_002[/eluser]
 
Further tests have revealed $this->load->vars(array()); if used in a controller's __construct() make all the array variables available in that particular controller methods and called views.

Since my dynamic $today array is used in nearly every view and numerous controllers, it seems sensible to declare a unique public $today variable in the common MY_Controller class.

The conclusion reached is that individual view variables can be set in a particular controller and these will be passed to the called view.

I think that I will stick to using the $this->load->view('view', $data, TRUE); technique but later will no doubt think of a benefit when the load->var tool will be useful.
 
 
#15

[eluser]DrDave[/eluser]
I too have been struggling with this for some time. I was finally able to get the variables available to all views using MY_Controller, but they are not available in any sub-controllers. There is one controller where I need to be able to make a modification to the categories. Is there a way to access this variable in that controller?


MY_Controller
Code:
class MY_Controller extends CI_Controller {

    public $view_data = array();

    public Function __construct()
    {
        parent::__construct();
        
        $this->load->model('site_data');
        $view_data['categories'] = $this->site_data->get_categories();
        $this->load->vars($view_data);
    }

}
#16

[eluser]John_Betong_002[/eluser]
Read and digest my previous threads where I mentioned that vars loaded in MY_Controller->__construct() are visible in your views but not visible in your controllers.


Try this:

// one_controller.php
Code:
function whatever()
{
  ...
  ...
  ...
  // copied from MY_Controller.php
  $this->load->model('site_data');
  $view_data['categories'] = $this->site_data->get_categories();

  // your code to modify $view_data
  $view_data['test_001'] = 'this is a test to see if it works';
  $this->load->vars($view_data);

  $data['page_title'] = 'Title goes here';

  $this->load->view('view', $data, FALSE);
}
 
 
#17

[eluser]InsiteFX[/eluser]
Here is something that I have been playing with, still working on it but it does work!
You can also store array's if you serialize and unserialize them.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class        Registry
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Libraries
* @author        Raymond King aka:(InsiteFX).
* @copyright    Copyright (c) 2005 - 2011, Advanced Computer Systems, LLC.
* @license        http://www.learnellislab.com/codeigniter/user-guide/license.html
* @link        http://www.learnellislab.com/codeigniter/user-guide/libraries/name.html
* @since        Version 1.0.0
*/

// Autoload this library in application/config/autoload.php

class Registry {

    /**
     * Class Variables - protected, private, public and static variables.
     * --------------------------------------------------------------------
     */
    private static $registry = array();


    // --------------------------------------------------------------------

    /**
     * __construct()
     *
     * Class    Constuctor PHP 5+
     *
     * @access    public
     * @param    array
     * @return    void
     */
    public function __construct($config = array())
    {
        // NOTE: if you use a config file it must be named the same as this Class!
        // application/config/registry.php
        foreach ($config as $key => $value)
        {
            if ( ! self::exists($key))
            {
                self::$registry[$key] = $value;
            }
        }
    }

    // --------------------------------------------------------------------

    /**
     * add() - Example: $this->registry->add($key, $value);
     *
     * Adds an item to the registry array
     *
     * @access    public
     * @param    string    - item's unique key
     * @param    mixed    - value
     * @return    boolean
     */
    public function add($key, $value)
    {
        if ( ! self::exists($key))
        {
            self::$registry[$key] = $value;
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    // --------------------------------------------------------------------

    /**
     * exists() - Example: $result = $this->registry->exists($key);
     *
     * Returns TRUE if item is registered in the registry array
     *
     * @access    public
     * @param    string    - key name
     * @return    boolean
     */
    public function exists($key)
    {
        if (is_string($key))
        {
            return array_key_exists($key, self::$registry);
        }

        return FALSE;
    }

    // --------------------------------------------------------------------

    /**
     * get() - Example: $this->registry->get($key);
     *
     * Returns the registered key value
     *
     * @access    public
     * @param    string    - key name
     * @return    mixed    - (FALSE if key name is not in registry array)
     */
    public function get($key)
    {
        if (self::exists($key))
        {
            return self::$registry[$key];
        }

        return FALSE;
    }

    // --------------------------------------------------------------------

    /**
     * delete() - Example: $result = $this->registry->delete($key);
     *
     * delete's a registry entry
     *
     * @access    public
     * @param    string    - key name
     * @return    boolean
     */
    public function delete($key)
    {
        if (self::exists($key))
        {
            unset(self::$registry[$key]);
        }

        return TRUE;
    }

    // --------------------------------------------------------------------

    /**
     * clear() - Example: $this->registry->clear();
     *
     * Clears the entire registry array
     *
     * @access    public
     * @return    boolean
     */
    public function clear()
    {
        self::$registry = array();
    }

    // --------------------------------------------------------------------

    /**
     * debug() - Example: echo var_dump($this->registry->debug());
     *
     * Return's the entire registry array for debugging
     * @access    public
     * @return    array
     */
    public function debug()
    {
        return self::$registry;
    }

}


// ------------------------------------------------------------------------
/* End of file Registry.php */
/* Location: ./application/libraries/Registry.php */

InsiteFX
#18

[eluser]CodyPChristian[/eluser]
Not to revive a older thread but I dont see any of you trying the method that I use:

http://learn-codeigniter.com/blog/passin...ded-views/

Hope that helps




Theme © iAndrew 2016 - Forum software by © MyBB