SELECT DYNAMIC CODEIGNITER - AJAX

1

Hello! I have the following tables:

tbl_encargo
tbl_category
tbl_encargo

Since in tbl_lacamento there is a field ( id_categoria ) that receives the ids of tbl_categoria and tbl_categoria receives its id ( id_juros ), coming from the tbl_juros table.

I am using codeigniter and I want to display in a field hidden <input type="hidden" id="juros" name="txt_juros" placeholder="juros" title="juros"/> in view lançamento all categories of tbl_categoria , and I want to dynamically display all ids_juros related to tbl_categoria .

Can you lead me to some code that does this? I think this would work for ajax, I tried to relate by following an example of states and cities, but I could not because the related ids are in the parent table.

What I have today, displays only categories. Below a view

<select id="categoria" class="form-control" name="sel_id_categoria" >
    <?php foreach ($categoria as $categoria) {
       echo '<option value="'.$categoria->id.'">'.$categoria->descricao.'</option>';
    } ?>
</select>

Controller code launching

//Listar Categorias ativas Receitas e Despesas
$this->load->model('app/Categoria_model');
$this->data['categoria'] = $this->Categoria_model->obter_ativo('tbl_categoria', 'tbl_categoria.id,tbl_categoria.descricao');

Code no model Category_model

	//Listar ativos
  var $tabela = 'tbl_categoria';	
	function obter_ativo($tabela,$campos)
	{        
        $this->db->select($campos);
        $this->db->from($tabela);
        $this->db->where('situacao',1);
        $query = $this->db->get();
        return $query->result();;
  }

-- -----------------------------------------------------
-- Table 'tbl_lancamento'
-- -----------------------------------------------------
DROP TABLE IF EXISTS 'tbl_lancamento';
CREATE TABLE IF NOT EXISTS 'tbl_lancamento'(
  'id' INT(11) NOT NULL AUTO_INCREMENT,
  'tipo' TINYINT(1) NOT NULL DEFAULT 0,
  'dt_lancamento' DATETIME NULL DEFAULT NULL,  
  'descricao' VARCHAR(255) NULL DEFAULT NULL,
  'valor' decimal(10,2) NOT NULL,
  'dt_vencimento' DATE NOT NULL,
  'id_categoria' INT(11) NULL DEFAULT NULL,
  'id_juros' INT(11) NULL DEFAULT NULL,
  'id_conta' INT(11) NULL DEFAULT NULL,
  'baixado' TINYINT(1) NOT NULL DEFAULT 0,
  'valor_pagamento' decimal(10,2) NOT NULL,
  'dt_pagamento' DATE NULL DEFAULT NULL,  
  'forma_pagamento' VARCHAR(100) NULL DEFAULT NULL,
  'estornado' TINYINT(1) NOT NULL DEFAULT 0,
  'observacao' TEXT(160) NULL DEFAULT NULL,
  PRIMARY KEY ('id'),
  INDEX 'fk_tbl_lancamento_tbl_conta' ('id_conta' ASC),
  CONSTRAINT 'fk_tbl_lancamento_tbl_conta'
  FOREIGN KEY ('id_conta')
  REFERENCES 'tbl_conta' ('id')
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
  )
ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS 'tbl_categoria';
CREATE TABLE IF NOT EXISTS 'tbl_categoria' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'descricao' varchar(45) NOT NULL,
  'tipo' TINYINT(1) NOT NULL,
  'id_juros' TINYINT(1) NOT NULL,
  'aplicar_juros' TINYINT(1) NOT NULL,
  'situacao' TINYINT(1) NOT NULL,  
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS 'tbl_encargo';
CREATE TABLE IF NOT EXISTS 'tbl_encargo' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'descricao' varchar(45) NOT NULL,
  'juros' decimal(10,2) NOT NULL,
  'multa' decimal(10,2) NOT NULL,
  'funcao' varchar(250) NULL DEFAULT NULL,
  'situacao' TINYINT(1) NOT NULL,  
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
asked by anonymous 19.06.2017 / 23:04

1 answer

2

I would do it as follows:

<select id="categoria" class="form-control" name="sel_id_categoria" >
    <?php foreach ($categoria as $categoria) {
       echo '<option value="'.$categoria->id.'">'.$categoria->descricao.'</option>';
    } ?>
</select>

<input type="hidden" name="juros" id="juros" value="">


<script>
    $("#categoria").change(function(){
        var categoria = $("#categoria").val();
            $.ajax({
                url: "ajax/buscar_juros", // Metodo de buscar juros
                type: "POST",
                data: {categoria:categoria},
                success: function(data){
                    $("#juros").val(data);
                }
            });
    });
</script>

ajax / seek_rights

public function buscar_juros(){
    $this->db->where('categoria', $this->input->post('categoria'));
    echo $this->db->get("juros")->row('taxa_juros');
}
    
20.06.2017 / 17:13