Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
how to return count_all_results to ajax success function in codeiginter

I am trying to get the count of records and return the result number to my ajax success function to print it or get the value on the textbox.

the controller function is:

public function get_subaccount_count($id){ 
    $data = $this->accounts_model->get_count($id);
    echo json_encode($data);
}

and the model function:

public function get_count($id){
    $sql="SELECT COUNT(*) FROM $this->tablename";
    $this->db->where('node_parent',$id);
    return $this->db->count_all_results($this->tablename);  
}

and the javascript in the view is:

<script>
$('#sub_account').change(function(){
    var accountcode = document.getElementById("main_code").value;
    var subaccount = document.getElementById("sub_account").value;
    //-------- loop to get count of sub accounts and add sum +1 ------------
    $.ajax({
        type: 'GET',
        url: "<?php echo base_url();?>index.php/accounts/get_subaccount_count/"+$('#main_code').val(),
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            $('#account_code').empty();
            $.each(result.result, function(){ 
                $('#account_code').value = this[0];
            })
        }  
    });
    //-------- loop to get count of sub accounts and add sum +1 ------------
});
</script>

I want the $('#account_code').value = to be populated by the count result.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your controller only needs to call the model method and print the returned integer.

public function get_subaccount_count(int $id): void
{ 
    echo $this->accounts_model->get_count($id);
}

Your model doesn't benefit from the $sql declaration.

public function get_count(int $id): int
{
    return $this->db->where('node_parent',$id)->count_all_results($this->tablename);  
}

Your ajax block only needs to assign the printed value to the desired HTML element.

$.ajax({
    type: 'GET',
    url: "<?php echo base_url();?>index.php/accounts/get_subaccount_count/" + $('#main_code').val(),
    success: function (response) {
        $('#account_code').value = result;
    }
});

Or use $.get like this:

$.get("<?php echo base_url();?>index.php/accounts/get_subaccount_count/" + $('#main_code').val(), function(result) {
    $('#account_code').value = result;
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Change like this:

$.ajax({
            type: 'GET',
            url: "<?php echo base_url();?>index.php/accounts/get_subaccount_count/"+$('#main_code').val(),
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
              $('#account_code').empty();
              var counter = 0; // this line
              $.each(result.result, function(){ 
                counter++; //this line
                })}
              $('#account_code').value = counter; // and this line
            
        });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda