Welcome Guest, Not a member yet? Register   Sign In
Global variable in Controller
#1

[eluser]JimmyJ[/eluser]
Hey Folks

I'm trying to set-up global variables inside my controller for easy configuration purposes.

So for example, I could have a global variable kept in a database for the amount of rows_per_page to show during pagination.

Currently I have:
Code:
<?php

class Admin extends Controller {

    function Admin()
    {
        
            parent::Controller();    
            
            $global = array();
            
            $this->load->model('user_model');
            $this->load->model('content_model');
            $this->load->model('messaging_model');
            $this->load->model('config_model');
            
            $this->data['config'] = $this->config_model->getConfig();
    }
    
    function index()
    {
        $data['content'] = 'admin/home';
        $this->load->view('templates/admin', $data);
    }

I'd like to be able to take the fields in config_model and apply them to functions further down the page.

Something similar to $rows_per_page = GET_CONFIG_DATA

Then, in a function have:
Code:
// Sample pagination option
$config['total_rows'] = $rows_per_page;

Does anyone know how to accomplish this? I'm using PHP5 Smile
#2

[eluser]daBayrus[/eluser]
why store it in a database? you can set it in your config file like so,

Code:
$config['rows_per_page'] = 10;

and retrieve it anywhere by

Code:
$rows_per_page = $this->config->item('rows_per_page');
#3

[eluser]JimmyJ[/eluser]
It would be nicer to come from the database so I can give the client specific options to set globally for the admin and front-end.
#4

[eluser]mdvaldosta[/eluser]
I would put the config stuff in a helper or library, whos values are updated from the database, and just call it when you need it.
#5

[eluser]jedd[/eluser]
[quote author="JimmyJ" date="1293057192"]It would be nicer to come from the database so I can give the client specific options to set globally for the admin and front-end.[/quote]

How about using [url="/user_guide/libraries/sessions.html"]Session data[/url] to store this information? You can keep the canonical version in the user table of your database, and extract it on authentication, store it as session data, then retrieve it in that 'global variable' kind of way that you've described.
#6

[eluser]metaltapimenye[/eluser]
i assume, u just make kind of stand alone application engine. does use custom config acceptable? my personal reverence might be look like this..
Code:
#@ system/application/config/config.php ..if u want to exclude it as an external config file create it at system/application/config/config_external.php
$config['default_per_page'] = '30';

#load in model/controller/view
$this->config->load('config_external');

#fetch
$this->config->item('default_per_page');
...RTFM ..if u have time




Theme © iAndrew 2016 - Forum software by © MyBB