Total number of count fetching issue in codeigniter3

43 Views Asked by At

The issue is im trying to fetch the total counts of institution and display the count, im using pagination. but i got the total count result as NaN(not a number) why? how can i fix this issue?

cotroller

public function pagination_institutions()
    {
        $data = $this->data;

        $limit = 2; // Number of records per page
        $offset = $this->uri->segment(3) !== false && is_numeric($this->uri->segment(3)) ? (int) $this->uri->segment(3) : 0; // Validate and convert to integer

        // Retrieve paginated institution data
        $result = $this->homepage->select_pagination_institutions($limit, $offset);
        $data['institution'] = $result['data']; // Updated institution data
        $data['pagination'] = $result['pagination'];

        // Other data retrieval for institutions
        $data["incat"] = $this->homepage->get_all_in_category();

        if ($this->session->userdata('client_id') == 19) {
            // $data['total_institutions'] = $this->homepage->get_total_institutions_for_client(19);
            $total_institutions = $this->homepage->get_total_institutions_for_client(19);
            $data['total_institutions'] = is_numeric($total_institutions) ? $total_institutions : 0;
            // $data['total_institutions'] = $total_institutions;
        }
        
        $this->template->views($data["theme"] . '/institutions', $data);
    }

Model

public function select_pagination_institutions($limit, $offset)
    {
        // $client_id = 17; // Assuming you want client_id to be 17
        $client_id = Client_Id;

        // Apply pagination
        $this->db->limit($limit, $offset);
        $this->db->order_by('institution_id', 'DESC'); // Assuming 'institution_id' is the primary key

        // Set the conditions for the specific client_id
        $multiplewhere = array('sr_institution.status' => 1, 'sr_institution.client_id' => $client_id);

        $this->db->select('sr_institution.*,ci_institution_category.ic_name');
        $this->db->where($multiplewhere);

        // echo $this->db->last_query();

        $this->db->join('ci_institution_category', 'ci_institution_category.ic_id=sr_institution.category', 'left');

        // Fetch paginated data
        $result['data'] = $this->db->get('sr_institution')->result();

        // Count total rows with applied conditions
        $total_rows = $this->db->where($multiplewhere)->count_all_results('sr_institution');

        // Pagination Configuration
        $config['base_url'] = base_url('home/pagination_institutions');
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $limit;
        $config['reuse_query_string'] = TRUE;


        $this->pagination->initialize($config);

        // Set the pagination key in the result array
        $result['pagination'] = $this->pagination->create_links();
        

        return $result;
    }
public function get_total_institutions_for_client($client_id)
{
    // Set the conditions for the specific client_id
    $multiplewhere = array('status' => 1, 'client_id' => $client_id);

    try {
        // Count total rows with applied conditions for the specified client ID
        $total_rows = $this->db->where($multiplewhere)->count_all_results('sr_institution');
        return $total_rows;
    } catch (Exception $e) {
        // Handle the exception, you may log it or return a default value
        error_log("Error counting total institutions: " . $e->getMessage());
        return 0; // Return 0 or handle the error appropriately
    }
}

View

<h2>
                                <span class="number">
                                    <?php
                                    echo is_numeric($total_institutions) ? $total_institutions : 'N/A';
                                    // Add debugging here to see the actual type of $total_institutions
                                    var_dump($total_institutions);
                                    ?>
                                </span>+
                            </h2>

The issue is im trying to fetch the total counts of institution and display the count, im using pagination. but i got the total count result as NaN(not a number) why? how can i fix this issue?

0

There are 0 best solutions below