Welcome Guest, Not a member yet? Register   Sign In
Global variables
#1

[eluser]M01[/eluser]
Hi,

Is there any way to define a global variable and change it if needed in controllers or views?

I created a helper defining some global variables:

$GLOBALS['id'] = 0;

And then I created a function in the same helper to update the global variable when needed:

function change_id($id){
$GLOBALS['id'] = $id;

}

If I try to update the variable, it updates inside the method, but if I call it from another method, he returns the old id.

Thanks.
#2

[eluser]missionsix[/eluser]
bad idea.

goes against Object Oriented programming ideas.

If you need 'global' constants, use configuration variables, otherwise try and keep things in scope.
#3

[eluser]M01[/eluser]
I was trying to pass ids without posting them in the url, I wanted to leave a clean url...

Is there any other way to do this without global variables?
#4

[eluser]Colin Williams[/eluser]
Well, sorry to burst your bubble, but globals aren't retained across requests. That's what sessions are for. Also, you should seek to have unique URLs. That's what the "U" in URL means. Maybe if you clarify what you are trying achieve we can provide a good solution for you.
#5

[eluser]M01[/eluser]
I was trying to pass Ids to define menu sections, instead of using:

http://link/controller/method/1/2

I wanted to have:

http://link/menu_section_name

by routing the link, and passing the ids somehow.

How can I do this?

Thank you.
#6

[eluser]Derek Allard[/eluser]
You could create variables that last across page loads per user by using sessions.
#7

[eluser]wowdezign[/eluser]
I often use $this->uri->segment(n) in my models to determine where I am in the application.

Caution: For this to work, the URLs need to be consistent, or you can write one method in the model to sort it all out when making the decisions.

Often times, when I create a multi-category site with subcategories I just use the above technique with a specific naming convention:

In my database I have fields for controller, function, param1, and param2. Then in my model, I use a query something like:

Code:
if($this->uri->segment->(4)){
    $where['param2'] = $this->uri->segment->(4);
}
if($this->uri->segment->(3)){
    $where['param1'] = $this->uri->segment->(3);
}
if($this->uri->segment->(2)){
    $where['function'] = $this->uri->segment->(2);
}else{
    $where['function'] = 'index';
}
if($this->uri->segment->(1)){
    $where['controller'] = $this->uri->segment->(1);
}else{
    $where['controller'] = 'defaultControllerName';
}
$this->db->where($where);
$query  = $this->db->get('pages');


Maybe there's a better way to do it, but this is easy for me and I am the only one that has to edit the code, so it really isn't a big deal. Smile
#8

[eluser]Colin Williams[/eluser]
For heavily customized routing (like looking up aliases in a database table), look into overloading the Router class and/or using automagic functions like "_remap()" in your controller class.
#9

[eluser]M01[/eluser]
I'll try using an array, and i'll take a look on _remap().

Thanks for your suggestions, I thought that was already a way, because it's possible in wordpress and drupal.

Thanks!
#10

[eluser]Colin Williams[/eluser]
Code:
it’s possible in wordpress and drupal

Well, those are CMSs and they would be worthless without such a feature. CI is a framework, and you can build that functionality into your app via the avenues we mentioned. Good luck




Theme © iAndrew 2016 - Forum software by © MyBB