• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function call by getting the name of function from URL


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

$function_name = explode("projects/", $_SERVER['HTTP_REFERER']);
It gives me name of function , now i've problem how to call this function in controller.
I want to use something like that


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

[eluser]Thorpe Obazee[/eluser]
I can't understand what's going on. But you could use $this->router->method() to get the current method.

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.


[eluser]Thorpe Obazee[/eluser]
I'd use sessions to keep track of these things.

can you tell me how to do this using sessions.

suppose this is url


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.

Doesn't CI automatically call function 'site_plan' from your controller 'projects' or am I misunderstanding the question?

It automatically calls, but now i want to call it now in this way...

[eluser]Phil Sturgeon[/eluser]
Three ways to call a function from a variable name in PHP.

Option #1:


Option #2:

call_user_func(array($this, $function_name[1]), $param1, $param2, $param3);

Option #3:
$params = array( $param1, $param2, $param3 );
call_user_func_array(array($this, $function_name[1]), $params );

[eluser]GSV Sleeper Service[/eluser]
your example

this *should* work, no guarantees, buyer beware etc.

I'm not sure how you have your views setup, but let's assume you have a separate view for your search form:
$this->load->view('header', $data);
$this->load->view('search_form', $data);
$this->load->view('search_results', $data);
$this->load->view('body', $data);
$this->load->view('footer', $data);
In your class constructor, you can check to see if a search has been submitted and do your searching, like so:
function Classname()
          $data['search_results'] = $this->Your_model->search($this->input->post('search_keywords'));

Then in your search_results view you can check to see if there are search results, and only display something if true:
//search_results view

     foreach($search_results as $result)
          echo $result;


Then you should set your search form to submit to the current page:
//current_url() function requires url helper, e.g. $this->load->helper('url');

<form action='<?php echo current_url(); ?>' method='post'>
search:<input type='text' name='search_keywords'/><input type='submit'/>
This way, your search form will always submit to the current page, and any function within the class will always check for search submissions via the constructor, and the search_results view will only show search results if available.

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:
//search view

<form action='<?php echo current_url(); ?>' method='post'>
search:<input type='text' name='search_keywords'/><input type='submit'/>


foreach($_POST as $key => $value)
     echo "<input type='hidden' name='$key' value='$value'/>";



Make sense?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.