Welcome Guest, Not a member yet? Register   Sign In
function call by getting the name of function from URL
#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?


Messages In This Thread
function call by getting the name of function from URL - by El Forum - 06-16-2009, 01:18 AM
function call by getting the name of function from URL - by El Forum - 06-16-2009, 01:25 AM
function call by getting the name of function from URL - by El Forum - 06-16-2009, 02:51 AM
function call by getting the name of function from URL - by El Forum - 06-16-2009, 03:07 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 08:16 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 09:06 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 09:21 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 09:24 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 09:24 AM
function call by getting the name of function from URL - by El Forum - 06-18-2009, 09:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB