setting variables in controller confusion - 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 in controller confusion (/showthread.php?tid=18407) |
setting variables in controller confusion - El Forum - 05-05-2009 [eluser]september28[/eluser] Hi there, Just wondering if i have 2 functions in a controller say add and add2, could i set a variable in add and then reuse it in add2?? eg. Code: function add() { In my particular example each function is a different stage of a form and i only want to have to define the id for a record once and then reuse it at various stages of the form (add, add2 and add2) Thnaks in advance setting variables in controller confusion - El Forum - 05-05-2009 [eluser]TheFuzzy0ne[/eluser] Yes. That's the beauty of PHP objects! setting variables in controller confusion - El Forum - 05-05-2009 [eluser]september28[/eluser] That's what I thought but it doesn't seem to work... I have a form in the view that corresponds to the add function. In this view, the form submits to the url /link/add2 (link is the controller name). When I try to use $this->myvariable in add2 function it doesn't recognise the $this->myvariable.... Is it because there are 2 browser calls to the class so the object is created separately twice? Or do I need to declare the variable in the constructor function? E.g. var $myvariable Cheers setting variables in controller confusion - El Forum - 05-05-2009 [eluser]Dam1an[/eluser] You're right, the variables get reset because each function in the controller (page) is a seperate browser request. if you need to maintain this information accross pages, you could either set it in the constructor, or use sessions |