Welcome Guest, Not a member yet? Register   Sign In
function call by getting the name of function from URL
#1

[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']);
It gives me name of function , now i've problem how to call this function in controller.
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
#2

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

[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
#4

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

[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.
#6

[eluser]n0xie[/eluser]
Doesn't CI automatically call function 'site_plan' from your controller 'projects' or am I misunderstanding the question?
#7

[eluser]alberta[/eluser]
It automatically calls, but now i want to call it now in this way...
#8

[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 );
#9

[eluser]GSV Sleeper Service[/eluser]
Code:
your example
$this->’$function_name[1]’;

this *should* work, no guarantees, buyer beware etc.
$this->{$function_name[1]};
#10

[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);
$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:
Code:
function Classname()
{
     parent::Controller();
     if($this->input->post('search_keywords'))
     {
          $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:
Code:
//search_results view
<?php

if(isset($search_results))
{
     foreach($search_results as $result)
     {
          echo $result;
     }
}

?>

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');

<form action='<?php echo current_url(); ?>' method='post'>
search:<input type='text' name='search_keywords'/><input type='submit'/>
</form>
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:
Code:
//search view

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

<?php

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

?>

</form>

Make sense?




Theme © iAndrew 2016 - Forum software by © MyBB