Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!