Welcome Guest, Not a member yet? Register   Sign In
CI Paging with searchresult
#1

[eluser]Unknown[/eluser]
Hi all,

I hope someone can point me in the right direction.

I have a searchform from which the result is send to the search-controller

I use a model to do a query and i also use paging to page the query result.

For page 1 all works fine (it uses the values of $this->input->post('field'))

When I click on page 2 in the paging, I got all the records of the table in the database, since my post-values from the original form are gone.

Is there an example i can use to find a solution ?

Controller:

$this->membermodel->sex = $this->input->post('sexsearch');
$this->membermodel->sexsearch = $this->input->post('sex');
$this->membermodel->relationtype = $this->input->post('relationtype');

$result = $this->membermodel->get($config[’per_page’],$this->uri->segment(4));

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

if($this->uri->segment(3) == '') $offset = 0;
else $offset = $this->uri->segment(3);

$limit = 4;
$offset*= $limit;

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

$config['uri_segment'] = $this->uri->segment(3);
$config['base_url'] = site_url('/search/result/');
$config['total_rows'] = $result['numrows'];
$config['per_page'] = $limit;
$config['num_links'] = 10;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';

$this->pagination->initialize($config);

$data['paging'] = $this->pagination->create_links();

Model:

function Membermodel()
{
// Call the Model constructor
parent::Model();
}


function get($limit, $offset) {

// BEGIN FILTER CRITERIA CHECK
// If any of the following properties are set before mdl_contacts->get() is called from the controller then we will include
// a where statement for each of the properties that have been set.
if ($this->id) {
$this->db->where("id", $this->id);
}

if ($this->pc) {
$this->db->where("pc", $this->pc);
}

if ($this->location) {
$this->db->where("location", $this->location);
}

if ($this->lang) {
$this->db->where("lang", $this->lang);
}

if ($this->burstaat) {
$this->db->where("burstaat", $this->burstaat);
}

if ($this->sexsearch) {
$this->db->where("sexsearch", $this->sexsearch);
}

if ($this->relationtype) {
$this->db->where("relationtype", $this->relationtype);
}

if ($this->hobbies) {
$this->db->where("hobbies", $this->hobbies);
}
// END FILTER CRITERIA CHECK

// We will display our results in order by last name and then first name.
$this->db->orderby("username");

// This will execute the query and collect the results and other properties of the query into an object.
$this->db->limit($limit,$offset);

$query = $this->db->get("member",$limit,$offset);

$result['numrows'] = $query->num_rows();
$result['query'] = $query->result;

return $result;
}

Thnx,

Peter
#2

[eluser]arian1[/eluser]
i am having a same issue.. hope someone will reply soon.
#3

[eluser]arian1[/eluser]
i did some more search in ci forum and found that
storing in session post values are showing good result ..
hope it will be helpful
#4

[eluser]luisandrade[/eluser]
Hi friends

I have the same problem.
There's a search form that sends to controller. The first time is ok. But when use pagination there isn't that first variable from search form.
I just tried use flashdata with keep_flashdata. So... it didn't bring the right results.
I can't use session because I already used all it's space and is a very dangerous try mix other information in my case.
Some body have any idea to solve this problem?

Thanks a lot for a help

Luis Andrade
Brasil
#5

[eluser]BeingDefined[/eluser]
same issue here...
#6

[eluser]adamp1[/eluser]
Sadly this is a common problem with using the CI uri segments. With normal PHP you could just pass the search filters around in the url using the GET method.

So really there is only 2 solutions:
1. Turn GET uri's on and use ?for=something&order=asc
2. Use a complex flashdata solution.

I am too trying to figure out the best way to handle passing filter results along, but so far havn't got the perfect solution.
#7

[eluser]xwero[/eluser]
If you don't feel comfortable putting the search options in a session you can use a database table with following fields: sessionid/userid, search_option, search_value.

You could add it to the url without using a query string index.php/search/found/id/1/pc/1 and in the found method you can do
Code:
function found()
{
   $params = $this->uri->uri_to_assoc(); // this will return array('id'=>1,'pc'=>1)
    if (array_key_exists('id',$params)) {
      $this->db->where("id", $params['id']);
    }

    if (array_key_exists('pc',$params)) {
      $this->db->where("pc", $params['pc']);
    }

    if (array_key_exists('location',$params)) {
      $this->db->where("location", $params['location']);
    }
    $this->db->limit($limit,$offset);

    $query = $this->db->get("member",$limit,$offset);
}
#8

[eluser]adamp1[/eluser]
I forgot about uri_to_assoc, might have to try that solution, only problem is still passing all that data in the uri which can look messy.

The clean way is with sessions but how to handle it is hard, I was thinking of storing a hash of the controller and then storing all relevant filter options in an array. This way if you navigate from one pagination table to another it won't try and apply the old filter results to the new table, which would happen if you just stored the data globably.

Only thing is then is when should that data expire? Expire too soon and you might be searching and loose your filters, expire too late and when you come back and want a new search you still have the old settings.
#9

[eluser]MadZad[/eluser]
FWIW, I'm generally opposed to having anything other than a session ID in a cookie, so the first thing I did when exploring CI is to find a way to keep session information in a database table. There are multiple solutions out there, but we ended up picking OBsession, which still works in CI 1.6.1. (couldn't use CI's new session library, alas)

It was pretty easy to get used to, and you can easily store whatever you want in the session, including search parameters or even the search results. Of course, heed adamp1's advice above...
#10

[eluser]luisandrade[/eluser]
Hi!
I just solved my problem . It's very simple and works fine. Using session. But it's necessary to keep attencion for max lenght of a cookie(4Kb). It's meaning around 4.000 characters. I think it's enough for any application.
Using CI 1.6.1 we don't need to use OBSESSION library because in this CI Version we already can use our variables.

Thanks folks

Luís Andrade
Brasil




Theme © iAndrew 2016 - Forum software by © MyBB