EllisLab text mark
Advanced Search
     
Where to declare global variables?
Posted: 14 May 2010 04:52 PM
Joined: 2010-05-14
4 posts

Hey CodeIgniters!

I just tried the CodeIgniter framework, and I’d say.. I am impressed. It’s lightness, and easiness.. Only after a couple of hours, I really feel that this definitely has a future in my coding..

Anyway, I am looking for a place where I can decare global variables.. The variable I need to declare is the name of my site? How would you declare that, so I can use it everywhere in my code, without using the: $data[‘title’]=“sitetitle”; in every single function?

Cheers! :D

 
Posted: 14 May 2010 05:23 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

Global variables shouldn’t be used in modern development, before you know it you have a maintenance nightmare on your hands.

CodeIgniter uses config files to define site-wide values. Check the documentation, Class reference, config class.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 14 May 2010 05:25 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-05-14
4 posts

So what would you do?

 
Posted: 14 May 2010 05:27 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

Eurhmm… Use a config file?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 14 May 2010 05:34 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-05-14
4 posts

That’ll be a global variable, and you said you wouldn’t use that..

 
Posted: 14 May 2010 05:45 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

Have you read the docs re. the config class?

A global variable in PHP is something completely different.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 14 May 2010 06:23 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-05-14
4 posts

Sorry man.. Didn’t read it yet.. I haven’t looked at the code yet, anyway. Thanks for your help! I bet it will work, currently not at my computer :D

Cheers!

 
Posted: 14 May 2010 10:52 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

If there are variables that you would like to set for 1 controller you can use

//Controller
class Something extends Controller{

  
var $somevar//Create a variable for the entire controller

  
function Something(){
     parent
::Controller();
     
     
$this->somevar['message''Something to store for every method';
     
$this->load->vars($this->somevar); //Load into every view
  
}

  
function mymethods(){
      
     $data[
'another_message'$this->somevar['message']//Or access variable in methods
     
$this->load->view('myview',$data); //Load $data keys into view
  
}

I use both. config files for what would be static variables to replace define(). Doing something dynamic like a breadcrumb, page title, or user id, I might use the method shown above by putting the code into the constructor of the class. Once you get your feet wet with CI you find you can take this to the next level, by extending the Controller class. You can create custom controller classes for public and private controllers (think frontend/backend). Here’s a great explanation.

 
Posted: 16 November 2010 12:16 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2010-11-16
1 posts

Hello Guys, hope someone can help me with this.
So, I have this some application I’m building with CI and I need some user-entered data to be saved until they are manually cleared by a button on the UI of the web application. So I essentially need variables that will stay alive until they are manually deleted.

One way to do this of course (which I have already implemented) is to save the variables to a file. But that means writing to the file, then reading from it. It’s slow. Another way, which I don’t want to use, is to save the variables in a db. Doing that would limit the portability of the app thou, i.e. I won’t be able to easily put it on any remote server for a friend to check out.

I’ve read about custom config files in CI which can hold data, and thou the data I wanted to save isn’t configuration data, the app is small, and I really just need something that can save and hold my data til I clear it, the way a file or a db would. The problem is that when I create a config file, save a user-entered piece of data to a config array variable in that file, and output the result to the browser, it’s fine, but when I enter another piece of data to that array variable and try to output its contents, it’s empty again.

So, any help would be very much appreciated!

 
Posted: 17 November 2010 06:38 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2006-12-26
727 posts
Jett323 - 16 November 2010 05:16 PM

Hello Guys, hope someone can help me with this.
So, I have this some application I’m building with CI and I need some user-entered data to be saved until they are manually cleared by a button on the UI of the web application. So I essentially need variables that will stay alive until they are manually deleted.

One way to do this of course (which I have already implemented) is to save the variables to a file. But that means writing to the file, then reading from it. It’s slow. Another way, which I don’t want to use, is to save the variables in a db. Doing that would limit the portability of the app thou, i.e. I won’t be able to easily put it on any remote server for a friend to check out.

I’ve read about custom config files in CI which can hold data, and thou the data I wanted to save isn’t configuration data, the app is small, and I really just need something that can save and hold my data til I clear it, the way a file or a db would. The problem is that when I create a config file, save a user-entered piece of data to a config array variable in that file, and output the result to the browser, it’s fine, but when I enter another piece of data to that array variable and try to output its contents, it’s empty again.

So, any help would be very much appreciated!

 
Take a look at sessions either PHP or the CodeIgniter equivalents.

Beware if you use PHP sesssions then session_start(); must be declared way before any HTML output. Once declared then you can set $_SESSION[‘user-entered_variable’] = $_POST[‘user-entered_variable’];
 
 
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

 
Posted: 17 November 2010 05:23 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-03-30
1317 posts

I would recommend using sessions as well. Just make sure that your session length will exceed the time expected for them to clear the data from your UI button.

 
Posted: 16 January 2011 01:39 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2009-03-04
149 posts

post7 is what I was looking for a long time.

 
Posted: 18 January 2011 11:18 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-06-11
18 posts

take note too that when using cookies (whether native PHP cookies or via CodeIgniter’s Helper function), you must refresh the page or redirect it to a new one for the
cookie values to be active, either way, it can easily be implemented in the controller.

e.g.

//controller: sample
function index()
{
    $this
->load>helper('cookie'); //can also be placed in the constructor
    
set_cookie('test_cookie','test',0);
    
redirect('sample/main');
}

function main()
{
    $this
->load>helper('cookie'); //can also be placed in the constructor
    
$cookie get_cookie('test_cookie');

    
//render page, i.e. load views, etc.