Welcome Guest, Not a member yet? Register   Sign In
Search and results
#1

[eluser]doubleplusgood[/eluser]
Hello there,

I have been struggling with a CodeIgniter search and I am hoping someone might be able to help me.

I have a simple form that has a dropdown menu for Regions. All I want to do is lookup the users table to find all users with the selected region from the dropdown and then display the results with pagination and display the number of records found.

This is my simple form that uses a dynamic list of regions;

Code:
<?=form_open('search/dealers')?>

    <select name="region">
        <option value="-">-</option>
        &lt;?php foreach($regions as $item) : ?&gt;
        <option value="&lt;?php echo $item['id']?&gt;">&lt;?php echo $item['name']?&gt;</option>
        &lt;?php endforeach; ?&gt;
    </select>

    <p><button class="button rightmini" type="submit">Submit</button></p>

&lt;?=form_close()?&gt;

My users table has; id, name and region (which would match up to the search parameter). I also have a search_model.php with the following functions;

Code:
public function getAllDealers(){
    $this->db->where("group_id", "1");
    $this->db->order_by("business_name", "asc");
    $query=$this->db->get('users');
    if($query->num_rows()>0){
        // return result set as an associative array
        return $query->result_array();
    }
}

public function getRegions(){
    $this->db->order_by("name", "asc");
    $query=$this->db->get('regions');
    if($query->num_rows()>0){
        // return result set as an associative array
        return $query->result_array();
    }
}

I'm wondering if I can pass the region id from the select menu into the getAllDealers function? Also wondering if anyone has a quick example of a results controller that has the pagination and total records count?


Thank you very much if anyone can help.
#2

[eluser]suba[/eluser]
in controller
---------------
Code:
function search_results()
{
$count = $this->db->count_all_results('table_name');
$this->load->library('pagination');
$config['base_url'] = 'http://10.71.152.73/sims/index.php/settings/user';        
$config['total_rows'] = $count;        
$config['per_page'] = 25;        
$config['next_link'] ='Next';        
$config['prev_link']='Prev';
$config['cur_tag_open'] = '<b style=\'color:green;padding-left:5px;\'>';
$config['cur_tag_close'] = '</b>';
$this->pagination->initialize($config);
//here you get & store values form your form using GET or POST
$this->db->where('your condition based on your form values);
$this->db->limit($config['per_page'],$limit);
$this->db->order_by('your values');
$this->data['query']=$this->db->get('table_name');
$this->data['si']=$limit; // serial number 1 ,2 ,3 etc.,
$this->load->view('serach_results_view',$this->data);
}

Then in your view page,
Code:
&lt;? foreach($query->result as $row) { ?&gt;
<tr><td>&lt;?=$row->field_name?&gt;</td></tr>
&lt;? } ?&gt;
// here you use your statement
#3

[eluser]doubleplusgood[/eluser]
Hey man,

Thanks very much for the reply. Here is my updated version;

Code:
function region_results()
    {
        $count = $this->db->count_all_results('users');
        $this->load->library('pagination');
        $config['base_url'] =  base_url().'index.php/search/dealers/';  
        $config['total_rows'] = $count;        
        $config['per_page'] = 25;        
        $config['next_link'] ='Next';        
        $config['prev_link']='Prev';
        $config['full_tag_open']= '<div id="pagination">';
        $config['full_tag_close']='</div>';
        $this->pagination->initialize($config);
        //here you get & store values form your form using GET or POST
        $region = $this->input->post('region');
        $this->db->where('region', $region);
        $this->db->limit($config['per_page'],$limit);
        $this->view_data['query']=$this->db->get('users');
        $this->view_data['si']=$limit;
        $view_data['view_file'] = 'search/dealers/region_results';
        $this->load->view('layout', $this->$view_data);

    }

Does this look about right to you?

Also wondered where $limit is set?

Thank you. Smile
#4

[eluser]doubleplusgood[/eluser]
Okay, I think I may be getting somewhere with this now. :S

So my view file with the form looks like this;

Code:
&lt;?=form_open('search/region_results')?&gt;
    <select name="region">
        <option value="-">-</option>
        &lt;?php foreach($regions as $item) : ?&gt;
        <option value="&lt;?php echo $item['id']?&gt;">&lt;?php echo $item['name']?&gt;</option>
        &lt;?php endforeach; ?&gt;
    </select> </p>

    <p><button class="button rightmini" type="submit">Submit</button></p>
&lt;?=form_close()?&gt;

I have the following function in my search.php controller;

Code:
function region_results()
    {
        $limit=10;
        $count = $this->db->count_all_results('users');
        $this->load->library('pagination');
        $config['base_url'] =  base_url().'index.php/search/dealers/';  
        $config['total_rows'] = $count;        
        $config['per_page'] = 25;        
        $config['next_link'] ='Next';        
        $config['prev_link']='Prev';
        $config['full_tag_open']= '<div id="pagination">';
        $config['full_tag_close']='</div>';
        $this->pagination->initialize($config);

        $region = $this->input->post('region');
        $this->db->where('business_region', $region);
        $this->db->limit($config['per_page'],$limit);
        $view_data['query']=$this->db->get('users');
        $view_data['si']=$limit;
        $view_data['view_file'] = 'search/region_results';
        $this->load->view('layout', $view_data);

    }

And then lastly, my region_results.php view file looks like this;

Code:
&lt;? foreach($query->result as $row) { ?&gt;
<tr><td>&lt;?=$row->business_name?&gt;</td></tr>
&lt;? } ?&gt;

&lt;?php echo $this->pagination->create_links(); ?&gt;

However, on this page I am getting the following errors;

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$result
Filename: search/region_results.php
Line Number: 1

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: search/region_results.php
Line Number: 1

I would be very grateful if anyone could suggest a solution. Thank you.
#5

[eluser]paulc010[/eluser]
It should be:

Code:
&lt;? foreach($query->result() as $row) { ?&gt;
<tr><td>&lt;?=$row->business_name?&gt;</td></tr>
&lt;? } ?&gt;

&lt;?php echo $this->pagination->create_links(); ?&gt;

Paul
#6

[eluser]doubleplusgood[/eluser]
Hey Paul,
Thanks for this. When I do a search now, it displays the results page without any errors, but doesn't show any data.

I guess there is something I have missed. Sad

Should the region select be passing the value number from the selected region or should it just pass the name of the select, i.e. 'region' ?
#7

[eluser]paulc010[/eluser]
Basically to use pagination you need to do the following:

1) Get the total number of results. (usually from our model)
2) Initialize the pagination library.
3) Get the data to display. (usually from our model)
4) Display the current page of results.

The Controller:

Code:
class Search extends Controller
{
    function region_results()
    {
        $region = $this->input->post('region');
        // Get the total number of results
        $this->db->where('business_region',$region);
        $query = $this->db->get('users');
        $count = $query->num_rows();
        $query->free_result(); // We're finished with this query, so free it

        $offset = $this->uri->segment(3, 0); // get the current pagination position if it exists, else 0

        $this->load->library('pagination');

        // Create our configuration
        // The same uri segment used above to get the "offset" note: THIS ISN'T THE PAGE #
        $config['uri_segment'] = 3;
        // "Parent" url to display results
        $config['base_url'] = base_url().'search/region_results';
        // The total number of rows to be paginated, calculated above
        $config['total_rows'] = $count;
        // Choose a number of results per page
        $config['per_page'] = 10; // Number of results you want per page

        // Configure the pagination library with our config array
        $this->pagination->initialize($config);

        // Now get the actual data to display i.e. the current "page" of results
        $this->db->select('business_name');
        $this->db->where('business_region',$region);
        $this->db->limit($config['per_page'], $offset); // Only get the results for this "page"
        $query = $this->db->get('users');

        $data['title'] = 'Results for search on : ' . $region;
        $data['results'] = $query->result();
        $this->load->view('layout', $data);
    }

}

The View:

Code:
<h1>&lt;?php echo $title; ?&gt;</h1>
<ul>
&lt;?php foreach ($results as $row) { ?&gt;
    <li>&lt;?php echo $row->business_name; ?&gt;</li>
&lt;?php } ?&gt;
</ul>
&lt;?php echo $this->pagination->create_links(); ?&gt;

I typed this up off the top of my head, so apologies in advanced if I've messed it up :cheese:

Note that this isn't the greatest code in the world since we get the # of results every page and there's lots you could do to make it more efficient. You may also want the search results to be bookmarked by the user, which this method doesn't support either.

Paul
#8

[eluser]doubleplusgood[/eluser]
Awesome. Thanks man that's great to get me going. Works spot on.




Theme © iAndrew 2016 - Forum software by © MyBB