Php – how to display result of count from the table field with codeigniter


sorry if my english was not good enough..
i'm getting stuck with my code, and don't know why it can be works..

this is my model function

    function counterItem(){
    $query = $this->db->query('SELECT SUM(barang_qty) FROM barang');

the function will count all value from barang_qty.

and then this my controller function

    function cListItem(){
    $data = array(
        'data_barang'   => $this->Model_App->getAllData('barang'),
        'data_tShirt'   => $this->Model_App->displayTshirt('barang', 'T-Shirt'),
        'data_shirt'    => $this->Model_App->displayShirt('barang', 'Shirt'),
        'data_pants'    => $this->Model_App->displayPants('barang', 'Pants'),
        'data_jeans'    => $this->Model_App->displayJeans('barang', 'Jeans'),
        'data_jacket'   => $this->Model_App->displayJacket('barang', 'Jacket'),
        **'total_item'  => $this->Model_App->counterItem(),**
    $this->load->view('admin/vBarang', $data);

The variable to get all value of my model's function is total_item.

and then this is my view's code

<table class="table table-hover">
            <th>Code Item</th>
            <th>Price /pcs</th>
                foreach($data_barang as $row){
            <td><?php echo $no++; ?></td>
            <td><?php echo $row->barang_kode; ?></td>
            <td><?php echo $row->barang_type; ?></td>
            <td><?php echo $row->barang_qty; ?></td>
            <td><?php echo $row->barang_satuan; ?></td>
            <td>Rp. <?php echo $row->barang_harga; ?></td>
                <a class="btn btn-xs btn-danger" href="<?php echo site_url(); ?>cAdminPage/cDelItem/<?php echo $row->barang_id; ?>"
                onclick="return confirm('Anda yakin akan menghapus data ini?')" data-toggle="tooltip" data-placement="left" title="Hapus"><span class="glyphicon glyphicon-trash"></span></a> 
                <a class="btn btn-xs btn-success" href="#modalEditBarang<?php echo $row->barang_id ?>" data-toggle="modal" data-hover="tooltip" data-placement="right" title="Edit"><span class="glyphicon glyphicon-edit"></span></a>
        <?php }
            <td>D</td> // the result of counter is here

I want to display the value of my query in Row D.
When i try to call the variable with

<?php echo $total_item ?>

but its not works.
where did i go wrong?
please advice,
thanks 🙂

Best Solution


Or if you only want to count the ids in table of entry in database you can use mysql function count in php function and return the value and send in array to view and get by value by array key.