Welcome Guest, Not a member yet? Register   Sign In
Where to declare global variables?
#1

[eluser]Mikkel Juhl[/eluser]
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! Big Grin
#2

[eluser]WanWizard[/eluser]
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.
#3

[eluser]Mikkel Juhl[/eluser]
So what would you do?
#4

[eluser]WanWizard[/eluser]
Eurhmm... Use a config file?
#5

[eluser]Mikkel Juhl[/eluser]
That'll be a global variable, and you said you wouldn't use that..
#6

[eluser]WanWizard[/eluser]
Have you read the docs re. the config class?

A global variable in PHP is something completely different.
#7

[eluser]Mikkel Juhl[/eluser]
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 Big Grin

Cheers!
#8

[eluser]pickupman[/eluser]
If there are variables that you would like to set for 1 controller you can use
Code:
//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.
#9

[eluser]Unknown[/eluser]
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!
#10

[eluser]John_Betong[/eluser]
[quote author="Jett323" date="1289945795"]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![/quote]
 
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'];
 
 
 




Theme © iAndrew 2016 - Forum software by © MyBB