How to use a variable in: $this->index(); ? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: How to use a variable in: $this->index(); ? (/showthread.php?tid=29602) |
How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]chefnelone[/eluser] Hello. I'm running this line in a controller: $this->index(); //it works fine But I need to use it in this way: $page = 'index'; $this->$page();// didn't work or $page = 'index()'; $this->$page; // didn't work How can I do this? How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]eoinmcg[/eluser] you should read up on OOP in php. Code: $this->page Code: $this->page Code: <?php How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]mddd[/eluser] The first example should work: Code: $page = 'index'; Is this literally what you are doing? If you use class properties, it's a bit different: Code: class Someclass() How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]mddd[/eluser] You could also user call_user_func(). Check the php manual for that. Example: Code: // call index() with argument 'hello' How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]chefnelone[/eluser] Ok, I see. I'll remake my question: Let's say I have 3 functions in a controller. When the index function is called it runs one of the other functions based in the value of $page. Code: function index(){ How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]mddd[/eluser] Didn't I just answer that question? Code: function index() Also: it looks to me like you are building functionality that CodeIgniter already has. Why do you always go to this index method? CodeIgniter is smart about routing stuff directly to the correct controller/method. You don't have to do that yourself. How to use a variable in: $this->index(); ? - El Forum - 04-15-2010 [eluser]chefnelone[/eluser] [quote author="mddd" date="1271340200"]Didn't I just answer that question? Also: it looks to me like you are building functionality that CodeIgniter already has. Why do you always go to this index method? CodeIgniter is smart about routing stuff directly to the correct controller/method. You don't have to do that yourself.[/quote] It works. I'm sure you are right, but I'm learning CI and now I need this working. But I don't know how to do what I need with routes. |