php – How to filter results and reload page/view in CodeIgniter?

Question:

I have code in CodeIgniter that takes a value from a select dropbox in one view and leads to a second view showing the results in a table. In this second view , I kept the dropbox and the submit button ("Filter") and I would like the user to be able to select another value from the dropbox and filter, reloading the page with other results to be shown in the table.
I did this but the page is not reloading. I believe I should put something at the end of the URL to be different with each refresh or something like that, but so far without success. Any suggestion?

Model:

 class TrackerModel extends CI_Model { function get_coordenadas($cliente, $filtro, $num, $offset){ //SELECT * FROM `tracker_coordenada`,`tracker_veiculo` WHERE tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo AND tracker_veiculo.cli_cod = $cliente $this->db->select('tracker_coordenada.*'); $this->db->from('tracker_coordenada, tracker_veiculo'); $this->db->where('tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo'); $this->db->where('tracker_veiculo.cli_cod',$cliente); if($filtro !=NULL){ $this->db->where('tracker_veiculo.descricao',$filtro); } $query = $this->db->get('',$num, $offset); return $query->result(); //$sql = "SELECT tracker_coordenada.* FROM `tracker_coordenada`,`tracker_veiculo` WHERE tracker_coordenada.veiculo_codigo = tracker_veiculo.veiculo_codigo AND tracker_veiculo.cli_cod = ".$cliente; //$resultado = $this->db->query($sql); //return $resultado->result_array(); } function conta_coordenadas($cliente){ //$query = $this->db->query("SELECT COUNT(ID) AS total FROM coordenadas WHERE news_id = $news_id"); //$row = $query->row(); //echo $row->total; $this->db->where('tracker_veiculo.cli_cod', $cliente); $this->db->where('tracker_veiculo.veiculo_codigo', 'tracker_coordenada.veiculo_codigo'); $query = $this->db->count_all('tracker_coordenada'); return $query; } function get_trackers($cliente){ $this->db->select('*'); $this->db->from('tracker_veiculo'); $this->db->where('tracker_veiculo.cli_cod',$cliente); $query = $this->db->get(); return $query->result(); } }

Controller:

 <?php class TrackerCtrl extends CI_Controller{ function __construct(){ parent::__construct(); $this->load->model('tracker/TrackerModel'); $this->load->model('FuncoesModel'); $this->load->library('table'); $this->load->language("label"); $this->load->helper('form'); } function index(){ if ($this->FuncoesModel->VerificaPermissao("listar", "tracker") != "S"){ redirect('principal/principalctrl/acessonegado'); } $dados['titulo'] = $this->lang->line('Tracker'); $cliente = $this->session->userdata("par_cli_codigo"); $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente); $dados['dinamico'] = '/tracker/TrackerView'; $dados['filtro'] = $this->input->post('frota_list'); $this->load->vars($dados); $this->load->view('/principal/PrincipalView'); } function listar($dados=""){ $dados['titulo'] = $this->lang->line('Tracker'); $cliente = $this->session->userdata("par_cli_codigo"); $dados['filtro'] = $this->input->post('frota_list'); $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente); $dados['dinamico'] = '/tracker/TrackerRelatorioView'; //Paginacao $config['base_url'] = base_url().'index.php/tracker/trackerctrl/listar/'; $config['total_rows'] = $this->TrackerModel->conta_coordenadas($cliente); $config['per_page'] = 5; $config['uri_segment'] = 4; $config['first_link'] = $this->lang->line('Primeiro'); $config['last_link'] = $this->lang->line('&Uacute;ltimo'); $config['next_link'] = $this->lang->line('Pr&oacute;ximo'); $config['prev_link'] = $this->lang->line('Anterior'); $dados['get_coordenadas'] = $this->TrackerModel->get_coordenadas($cliente, $this->input->post('frota_list'), $config['per_page'],$this->uri->segment(4)); $this->pagination->initialize($config); $dados["paginacao"] = $this->pagination->create_links(); $this->load->vars($dados); $this->load->view('/principal/PrincipalView'); } }

View:

 <? if($this->session->flashdata('msg')){ ?> <div style="background:#FF0000; text-align:center" align="center"> <font color="#FFFFFF"><?=$this->session->flashdata('msg')?></font> </div> <? } ?> <div class="row-fluid sortable"> <div class="box span12"> <div class="box-header" data-original-title> <h2><i class="icon-eject"></i><span class="break"></span><?=$titulo?></h2> <div class="box-icon"> <a href="#" class="btn-minimize"><i class="icon-chevron-up"></i></a> </div> </div> <div class="box-content"> <div class="control-group"> <label class="control-label" for="typeahead"><font color="#FF0000"></font><?=$this->lang->line('Ve&iacute;culo')?>:</label> <div class="controls"> <form id="form" name="form" method="post" action="<?=site_url('tracker/trackerctrl/listar/')?>"> <select name="frota_list" id="frota_list" style="width:300px" > <? foreach($user_trackers_list as $opcao){?> <option><?=$opcao->descricao?></option> <? }?> </select> <br><br> <button class="btn btn-small btn-primary" type="submit" id="btn_filtrar" name="btn_filtrar"><?=$this->lang->line('Filtrar')?></button> <?=form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash());?> </form> </div> </div> </div><!--/span--> </div><!--/row-->

Answer:

Your form points to the listar method of the TrackerCtrl class where the frota_list field is not considered when user_trackers_list the user_trackers_list variable through the get_trackers method of the TrackerModel class.

Perhaps you should change the get_trackers class's TrackerModel method to accept a second parameter (eg frota_item ) and add a second where clause to filter by that field (eg $this->db->where('tracker_veiculo.frota_item', $frota_item); ).

You can do this conditionally so that it doesn't change the current functioning. Ex.:

Model:

...
function get_trackers($cliente, $frota_item = false){
    ...
    $this->db->where('tracker_veiculo.cli_cod', $cliente);
    if($frota_item){
        $this->db->where('tracker_veiculo.frota_item', $frota_item);
    }
    $query = $this->db->get();
    ...
}
...

Controller:

...
function listar($dados=""){
    ...
    $dados['filtro'] = $this->input->post('frota_list');
    $dados['user_trackers_list'] = $this->TrackerModel->get_trackers($cliente, $this->input->post('frota_list'));
    $dados['dinamico'] = '/tracker/TrackerRelatorioView';
    ...
}
...
Scroll to Top