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

codeignitermysqlphp

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/header');
    $this->load->view('admin/vBarang', $data);
    $this->load->view('admin/footer');
}

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">
    <thead>
        <tr>
            <th>#</th>
            <th>Code Item</th>
            <th>Type</th>
            <th>Quantity</th>
            <th>Volume</th>
            <th>Price /pcs</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php
            $no=1;
            if(isset($data_barang)){
                foreach($data_barang as $row){
        ?>
        <tr>
            <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>
            <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>
            </td>
        </tr>
        <?php }
            }
        ?>
        <tr>
            <td>a</td>
            <td>b</td>
            <td>c</td>
            <td>D</td> // the result of counter is here
            <td>e</td>
            <td>f</td>
            <td>g</td>
    </tbody>
</table>

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

$result=$this->db->select_sum("barang_qty")->from("barang")->where()->get()-result();

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.