function call by getting the name of function from URL - 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: function call by getting the name of function from URL (/showthread.php?tid=19688) |
function call by getting the name of function from URL - El Forum - 06-16-2009 [eluser]alberta[/eluser] hello, I want to get the name of function from url and then call that function in controller. I have retrieve the name of function from url by using Code: $function_name = explode("projects/", $_SERVER['HTTP_REFERER']); I want to use something like that $this->'$function_name[1]'; it gives error Undefined property: Projects::$site_plan ( whereas site_plan is name of function) Can someone tell me how to call this function ?? Thanks in Advance function call by getting the name of function from URL - El Forum - 06-16-2009 [eluser]Thorpe Obazee[/eluser] I can't understand what's going on. But you could use $this->router->method() to get the current method. function call by getting the name of function from URL - El Forum - 06-16-2009 [eluser]alberta[/eluser] actually what em doing is, there is search form on each page of my website. when i click on search button it redirects me to index function of my controller, and from where after getting the result i want to show the result on previous page where i was before and show the result on same page. that's why i get the name of function from uri and ll call the same function and then pass same data plus additional search result. In fact if any one has some better idea to implement it, then do tell me. Thanks function call by getting the name of function from URL - El Forum - 06-16-2009 [eluser]Thorpe Obazee[/eluser] I'd use sessions to keep track of these things. function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]alberta[/eluser] can you tell me how to do this using sessions. suppose this is url Code: http://localhost/project/index.php/projects/site_plan here site plan is name of function. Now i want to call "this-site_plan();" in index() function how ll i do that?? i get the name of function from url in my index() function but don't know how to call it ?? Thanks in advance. function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]n0xie[/eluser] Doesn't CI automatically call function 'site_plan' from your controller 'projects' or am I misunderstanding the question? function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]alberta[/eluser] It automatically calls, but now i want to call it now in this way... function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]Phil Sturgeon[/eluser] Three ways to call a function from a variable name in PHP. Option #1: Code: $this->{$function_name[1]}(); Option #2: Code: call_user_func(array($this, $function_name[1]), $param1, $param2, $param3); Option #3: $params = array( $param1, $param2, $param3 ); Code: call_user_func_array(array($this, $function_name[1]), $params ); function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]GSV Sleeper Service[/eluser] Code: your example function call by getting the name of function from URL - El Forum - 06-18-2009 [eluser]slowgary[/eluser] I'm not sure how you have your views setup, but let's assume you have a separate view for your search form: Code: $this->load->view('header', $data); Code: function Classname() Then in your search_results view you can check to see if there are search results, and only display something if true: Code: //search_results view Then you should set your search form to submit to the current page: Code: //current_url() function requires url helper, e.g. $this->load->helper('url'); The only problem you might run into with this method is if the current page is generated using post data, then someone submits a search, the previous post data will not be there when the reloads. You could also solve this in your search view by iterating over post data and creating hidden fields for your search form, like so: Code: //search view Make sense? |