Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

How to filter admin model list by GET parameter?

edited August 2014 in Feature Requests
Hello guys,
I need help for admin side:

In 'one module' I have link -> fuel_url('another_module', array('id' => $id)), which produces URI -> '/admin/another_module?id=1', and on click at that link site goes to the 'another_module' page.

I need to develop auto filtration for items on 'another_module' page by GET parameters, in my example it is by $_GET['id].
I have extended list_items() method in the another_model.php file, but I can't get parameters from URI in it (((.

I have tried CI method $param_id = $this->input->get('id'), and on die($param_id); I can see it.
But when I add it to the WHERE condition, list_items() doesn't see it.

I do it like this:
----------
public function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc', $just_count = FALSE)
{
$param_id = $this->input->get('id')
$this->db->where(array('id' => $param_id)) ;

$data = parent::list_items($limit, $offset, $col, $order, $just_count);
return $data;
}
----------
I suppose it happens because of AJAX request to - "http://domain/admin/another_module/items/?search_term=&limit=50&view_type=list&offset=0&order=asc&col=name&fuel_inline=0".

Please help me. How can I get GET parameters in the _model files?
Thank you.

Comments

  • edited 4:36PM
    Have you looked at filtering. You can add a "filters" module parameter to your module in the fuel/application/config/MY_fuel_modules.php file similar to what is shown in the fuel/modules/fuel/config/fuel_modules.php file for the navigation module ($config['modules']['navigation'] = ...) . This could be a select of IDs or a hidden field (uses Form_builder syntax). These filters get sent in the $_GET requests automatically.
  • edited August 2014
    Can you create example, please. Have something similar and what I only found that fuel doing some redirection and my get params are in last_url();
    So using something like :

    /* I couldn't read chantiers_id from get method so I used last_url function to retrieve data what we sent with get method */ $last_url = last_url(); $params = explode( '?param =', $last_url); $count_array = count( $param ); if ( $count_array == 1) { $params = null; } elseif ( $count_array == 2 ) { $param = explode( '&', $params [1]); $param = $param [0]; };

    $param is that variable what I need to send and get. Also you may need how I sending, so I used simple:

    <li><a href="<?=fuel_url('another_module?param='.$id.'&param2=active', TRUE)?>" class="ico ico_view"><?=lang('btn_another_module')?></a></li>

    This is not the best way, but working if there any others solutions ?
  • edited 4:36PM
    You can try something like the following to add filters to your list view:

    In your fuel/application/config/MY_fuel_modules.php file, add the following filtering parameters to your modules configuration:
    $config['modules']['another_module'] = array( ... 'filters' => array( 'id' => array('type' => 'hidden'), 'other_param' => array('type' => 'select', 'options' => array('option1' => 'option1')), // additional example ... );
    This will create additional form fields in the list view that will get passed as $_GET parameters when rendering the list view. In this case, one will be a hidden field ("id") and the other will be a select list).
  • edited 4:36PM
    Hello Admin,
    I have added filters in the config/MY_fuel_modules.php file, and now I see hidden field in the list, and I see it in the AJAX request.
    But I still can't set value from Get ((.
    I'm trying to do this in another_module_model.php->list_items():
    public function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc', $just_count = FALSE)
    {
    $param_id = $this->input->get('id');
    parent::form_fields(array('id' => $param_id));
    $data = parent::list_items($limit, $offset, $col, $order, $just_count);
    return $data;
    }
    Please help me to resolve this.
  • edited 4:36PM
    have you tried

    global $_GET;
    echo $_GET['id'];

    i think all those $this->input->get('id') are just functions masked the fundamental php function. so the old way of writing php should be working...as long as the function is called.
  • edited 4:36PM
    yes, onegun, you are right, here is CI documentation about input class - https://ellislab.com/codeigniter/user-guide/libraries/input.html.

    My question is: how to set filter value in the _model.php file?

    The problem is because FUEL does an AJAX page reload after loading page by passed link. At first I see passed GET value, but after "AJAX page reload" thing I can't see required GET parameter, because I didn't set it on that AJAX request.
    That AJAX request automatically gathers filters values, so I need simply put my value into filter field before page does AJAX reload.
    I'm trying to do it in the _model->list_items() method, but FUEL documentation is poor and hard to understand for me (.
  • edited 4:36PM
    I'm not sure what may be going on but I know where I can point you to help debug. The BaseFuelController.js file has a submitForm method in it which is what is making the AJAX request. Note that that method sends the form data from $("#form") which should include any filter fields you have. If you console.log the params on line 678 (if latest version), do you see it in there?
Sign In or Register to comment.