![]() |
Setting variables to be used controller wide. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Setting variables to be used controller wide. (/showthread.php?tid=52636) |
Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]uber_n00b[/eluser] I want to setup a variable to be used by various functions in my controller. I have viewed one of the posts on here that offered a few suggestions but it was dealing with strings and not arrays. How can I make $timezone_drop; available to all functions in my class/controller? Code: $timezone_drop = array( I have tried placing it in my construct function Code: function __construct() index() will throw an error saying $timezone_drop is undefined however if I place the variable declaration in index() it works fine. Please help. Thanks! Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]LuckyFella73[/eluser] The contructor is the right place. Just write Code: $this->timezone_drop = array(); In your methods use the same way: Code: $my_array = $this->timezone_drop; Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]uber_n00b[/eluser] Thanks for the reply LuckyFella - I had tried that as well previously. This is the error I receive. A PHP Error was encountered Severity: Notice Message: Undefined variable: timezone_drop Filename: controllers/company.php Line Number: 7 Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]CroNiX[/eluser] PHP likes its variables declared before using them. Code: class Something extends CI_Controller { Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]uber_n00b[/eluser] Thank you for the help - it has been a long day and I made a stupid mistake - I was doing $this->$printer_drop as opposed to $this->printer_drop! Thanks again. Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]uber_n00b[/eluser] CroNIX - I assign it to another variable so I can pass $data[] to my view and use it to build a dropdown...Does that still not make sense? Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]CroNiX[/eluser] You can just use $this->timezone_drop directly in your view without explicitly passing it, since it's a class property and globally available to anything from within that controller. No need for taking up extra memory within a separate variable. Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]InsiteFX[/eluser] CroNiX showed you how to do it above Code: class Something extends CI_Controller { Setting variables to be used controller wide. - El Forum - 06-19-2012 [eluser]uber_n00b[/eluser] I understand that InsiteFX. CroNIX will give that a try in my view. Thanks again for the help. |