Welcome Guest, Not a member yet? Register   Sign In
Search & Pagination
#1

[eluser]Vasi[/eluser]
Hi,

I have a form with 3 inputs (1 text, 2 drop-downs). I have no problem performing the search and in displaying the pagination, the problem is in storing my POST values, on which the search query is based.

If I store them in the session, I keep getting the same result (of course) or nothing at all. So the questions is this:

How do I reset the session variables after I complete my search?
Or: If the session doesn't do the trick on storing the POST values, how should I rebuild my search module.

View
Code:
<?php
    $cauta = array(
        'name' => 'cauta',
        );
    $submit = array(
        'name' => 'search_button',
        'value' => 'Cauta'
                );
    $path = 'search';    
?>
<?=form_open($path);?>
<label><strong>Cauta:</strong>
&lt;?=form_input($cauta);?&gt;</label>
<label><strong>Oras:</strong>
&lt;?=$localitati_search?&gt; </label>
<label><strong>Categorie:</strong>
&lt;?=$categorii_search?&gt; </label>
<label>
&lt;?=form_submit($submit)?&gt; </label>
&lt;?=form_close();?&gt;

Controller
Code:
...
if($this->input->post('localitate'))
{
    $search_sess_loc = array(
        'search_loc' => $this->input->post('localitate')
        );
    $this->session->set_userdata($search_sess_loc);
}

if($this->input->post('categorie'))
{
    $search_sess_cat = array(
        'search_cat' => $this->input->post('categorie')
        );
    $this->session->set_userdata($search_sess_cat);
}
    
if ($this->session->userdata('search_loc'))
     $localitate = $this->session->userdata('search_loc');
else
    {
        $localitate = 0;
    }        
if ($this->session->userdata('search_cat'))
    $categorie = $this->session->userdata('search_cat');
else
{
    $categorie = 0;
}
    
if ($this->session->userdata('search_term'))
     $search_term = $this->session->userdata('search_term');
else
{
    $search_term = '';
}    
...
#2

[eluser]Twisted1919[/eluser]
Salut Smile
You could try like this :
Code:
function some_function($criteriul_1='all',$criteriul_2='all',$criteriul3='all')
{
    $count_results = $this->model->count_something($criteriul_1,$criteriul_2,$criteriul_3);
    [...]
}
I mean, give your function the params that you expect to have for search(if you have too many params go with func_get_args() ), and give them a default value,that will help you when you will make the pagination:
Code:
$config['base_url']        = site_url().'/controller/method/'.$criteriul_1.'/'.$criteriul_2.'/'.$criteriul_3.'/;
$config['uri_segment']     = 6;

The model function needs to look something like :
Code:
if($criteriul_1 != 'all'){
  //add here a query part that includes $criteriul_1
}
if($criteriul_2 != 'all'){
// same
}
if( $criteriul_3 != 'all' ){
//same
}

This example is only as an idea, just open your eyes, you will get it .
Let me know if you have question .
#3

[eluser]pickupman[/eluser]
You can shorten the syntax using ternary operators.
Code:
$localitate = ($this->session->userdata('search_loc')) ? $this->session->userdata('search_loc') : 0;
    
$categorie = ($this->session->userdata('search_cat')) ? $this->session->userdata('search_cat') : 0;

$search_term = ($this->session->userdata('search_term')) ? $this->session->userdata('search_term') : '';

Whenever you want to paginate search results you can do it one of 2 ways.
1.) Store the search params in the session like you are doing now. And then pass to your model for the results.
2.) Store the params in the DB. This is how it is done here on the boards. When you search, the terms are added to the DB, then the DB returns a record number, and you redirect the user to site.com/search/results/IDNumber. Similar to sessions you can add a timestamp to the record, and purge any searches older than a certain time. This allows allows you to cache the result of the search if someone else search the same term in a given time period.
#4

[eluser]Vasi[/eluser]
Thanks for the support guys! I really appreciate it.

Quote:1.) Store the search params in the session like you are doing now. And then pass to your
model for the results.

I am storing them now, but how do I unset them for other searches?


Thanks again
#5

[eluser]siubie[/eluser]
unset the session data Smile


$this->session->unset_userdata($array_items);

*from user guide
#6

[eluser]Vasi[/eluser]
Sorry, what I really meant to say was:
"When should I unset the session, because if I unset it after the search result is displayed, I loose the query for the other pages (of the pagination)."
#7

[eluser]n0xie[/eluser]
Why not just use GET? Makes more sense with paginating and filtering...
#8

[eluser]Vasi[/eluser]
[quote author="n0xie" date="1276868158"]Why not just use GET? Makes more sense with paginating and filtering...[/quote]

I tried using POST, in my view, like this:

Code:
if (!empty($_POST))
    {
        if (isset($_POST['cauta']))
        {
            $path =    'search/?term='.$_POST['cauta'];
            if (isset($_POST['localitate']))
                $path .= '&localitate;='.$_POST['localitate'];
            if (isset($_POST['categorie']))
                $path .= '&categorie;='.$_POST['categorie'];    
        }    
    }

The problem with this is that I get the right result after two clicks on the search button, on the first click I get zero results. I know why is this happening but I can't seem to find a solution.

Thanks again for the reply and support.

Cheers!
#9

[eluser]pickupman[/eluser]
You can do this this by posting form to a do_search() method, unset old data, set new data, and then redirect page to results() method.
#10

[eluser]siubie[/eluser]
[quote author="Vasi" date="1276864496"]Sorry, what I really meant to say was:
"When should I unset the session, because if I unset it after the search result is displayed, I loose the query for the other pages (of the pagination)."[/quote]

unset the search data only if there is new search query Smile, if not just continue with the paging




Theme © iAndrew 2016 - Forum software by © MyBB