Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Filtering results - how to re-set filter to show all
#1

[eluser]predat0r[/eluser]
Hi,
There is a part of a webpage, where products are listed. I have a dropdown to filter the products by manufacturers. I use ci sessions to do the filtering. If I select a manufacturer, it filters correctly the products, but if I select the first option 'All' - so show all products, it jumps back to the last selected manufacturer, (and filters products).
I can't figure out how to implement this default listings, maybe I tried the wrong way.
- When the page loads first time (no sessions stored) it starts by showing all products right.
- the option value is equal with the manufacturer's id.

My code:
contoller:

Code:
...
if(!$this->session->userdata('gyarto')) {
    $this->session->set_userdata('gyarto', '');    // first run default
    }
if($this->input->post('gyartok')) {
    $this->session->set_userdata('gyarto', $this->input->post('gyartok'));
    }
...

model:

Code:
...
if($this->session->userdata('gyarto')) {
    $filter = $this->session->userdata('gyarto');
    $this->db->where('manufacturer_id', $filter);    
}
...

View:

Code:
echo form_open('shop/cat/'.$category['id']);
    $options[''] = 'All';
    foreach($gyartok as $gyarto) {
        $options[$gyarto['id']] = $gyarto['name'];
    }
$js = 'onChange="this.form.submit();"';
echo form_dropdown('gyartok', $options, set_value('gyartok', $this->session->userdata('gyarto') ? $this->session->userdata('gyarto') : ''), $js);

echo form_close();

I tried in model:
1. if (input != '') then do filter
2. if (!empty(input)) then do filter
3. then I gave the first option a value like zero, same wrong result

Do you have an idea how to "reset" the filtering if I select 'All' as option?

Thank you!
#2

[eluser]LuckyFella73[/eluser]
I can't see an error looking at your code - maybe the error part
is missing in your post?

Just an idea how you could try to get the data (without using sessions):
Code:
# CONTROLLER
$man_value = $this->input->post('gyartok');
if (strlen($man_value)>0)
{
    $filter = $this->input->post('gyartok');
}
else
{
    $filter = '';
}

# call to your model
$data['gyartok'] = $this->your_model->your_method($filter);

# MODEL
function get_manufacturer($filter='')
{
    if (strlen($filter)>0)
    {
        $this->db->where('manufacturer_id', $filter);
    }
    // rest of your code goes here
}

Don't know if you really need to store the values into session, if not
I would prefer to do it without.
#3

[eluser]predat0r[/eluser]
Hi LuckyFella,

First, at the beginning I tried with url segments and I read some posts here that I can do it easily with session cookies. So I tried it, and for example the ordering works perfectly.

Just in time, I put a value of 9999 to the first Default option, and with this number showing all data - works! But not with 0 or '' .. strange.. Smile

So I solved my problem partly, but now I have to solve next thing, especially when I change category, my session remain live and no other products are shown.. LOLSmile

But, first I'll give it a try for your hint!

Thanks!
#4

[eluser]predat0r[/eluser]
I change temporarily my code to you suggestion, by filtering and sorting, too. Filtering is OK, but if I filter stg and then sort it, it sorts ALL the products not only the filtered products. So one step forth two steps back by now
#5

[eluser]LuckyFella73[/eluser]
Quote:... by filtering and sorting, too. Filtering is OK, but if I filter stg and then sort it, it sorts ALL the products not only the filtered products

It's hard to say without seeing the whole code. If you can
post the complete Model code (at least the method we are
talking about here) and the complete controller method the
chance would be much higher we can help you Smile
#6

[eluser]predat0r[/eluser]
[quote author="LuckyFella73" date="1297890000"]
Quote:... by filtering and sorting, too. Filtering is OK, but if I filter stg and then sort it, it sorts ALL the products not only the filtered products

It's hard to say without seeing the whole code. If you can
post the complete Model code (at least the method we are
talking about here) and the complete controller method the
chance would be much higher we can help you Smile[/quote]

sure, here they are

controller:
Code:
$man_value = $this->input->post('gyartok');
    if(strlen($man_value) > 0) {
        $filter = $this->input->post('gyartok');
    } else {
        $filter = '';    
    }
    
    $srd = $this->input->post('sorrend');
    if(strlen($srd) > 0) {
        $order = $this->input->post('sorrend');    
    } else {
        $order = '';    
    }
...
$data['listing'] = $this->MProducts->getProductByCategory($id, $config['per_page'], $this->uri->segment(5). $filter, $order);
...
model:
Code:
function getProductByCategory($catid, $limit, $offset, $filter = '', $order = '') {
    $data = array();
    $this->db->select('id,name,shortdesc,thumbnail,price,manufacturer_id');
    $this->db->where('category_id', $catid);
    $this->db->where('status', 'active');
    
    if(strlen($filter) > 0) {
        $this->db->where('manufacturer_id', $filter);
    }
    
    if(strlen($order) > 0) {
            switch($order) {
            case 'ASC' :
                $this->db->order_by('name', 'ASC');
                break;
            case 'DESC' :
                $this->db->order_by('name', 'DESC');            
                break;
            case 'PASC' :
                $this->db->order_by('price', 'ASC');
                break;
            case 'PDESC' :
                $this->db->order_by('price', 'DESC');        
                break;
            default:
                $this->db->order_by('name', 'ASC');
                break;
        }
        }

$this->db->limit($limit, $offset);        
$Q = $this->db->get('products');
...

So some way I have to get remembered the manufacturer for sorting..
#7

[eluser]LuckyFella73[/eluser]
Again I can't see an error here.

I would start to echo your post vars somewhere in your views
to check what values you get after submitting the form.

You said if you filter the results you get the wanted results
but when you set an order-rule the filter doesn't work anymore.
That only can happen if the $this->input->post('gyartok') is empty.

Put something like this at the top of your page and see what happens
after setting a filter and (after the page refreshed with filtered results)
an order.

Code:
<?php
echo 'POST VAR FILTER: '.$this->input->post('gyartok').'<br />';
echo 'POST VAR ORDER: '.$this->input->post('sorrend');
?&gt;
#8

[eluser]predat0r[/eluser]
[quote author="LuckyFella73" date="1297892700"]Again I can't see an error here.

I would start to echo your post vars somewhere in your views
to check what values you get after submitting the form.

You said if you filter the results you get the wanted results
but when you set an order-rule the filter doesn't work anymore.
That only can happen if the $this->input->post('gyartok') is empty.

Put something like this at the top of your page and see what happens
after setting a filter and (after the page refreshed with filtered results)
an order.

Code:
&lt;?php
echo 'POST VAR FILTER: '.$this->input->post('gyartok').'<br />';
echo 'POST VAR ORDER: '.$this->input->post('sorrend');
?&gt;
[/quote]

As I guessed, after ordering, the filter post gets clear. Yes its because - i think - it cant remember what was the previous post, thats because earlier i tried to put the filter post value in a ci session, but then comes a huge database error about category_id blabla..
commenting out the ci session thing error disappers
#9

[eluser]LuckyFella73[/eluser]
So everything works now? If not I would check the repopulating part where your select dropdown
is created (set_value ...)
#10

[eluser]predat0r[/eluser]
Not really

What works: I set the ci session in controller, there don't gets DB error message. So now, it remembers the filter, after playing with order dropdown.

What not: because of stored filter, when I go to other category, and then come back, the filter is set to the last set manufacturer, but all the products are listed. Maybe I can reset the filter session when the uri is changing?? Or..?

My second big pain in the a** I use CI pagination, I didnt tell that earlier. When I filter stg, I have <b>always</b> a second page, even if it's one item, dont know why..

Lets give an example:
I have filter, Corsair, Kingmax, Kingston, OCZ, Samsung memories, and 5 products ,too.
Pagination 'num_links' is set to 3.
Filtering Corsair, I become one product, but it is a second page with one OCZ and one Samsung RAM.
Have no idea what happens..




Theme © iAndrew 2016 - Forum software by © MyBB