How to display counting result from my table field using codeigniter

Sorry if my english was not good enough. I'm stuck with my code and don't know why this might be working.

this is my model function

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

      

the function will read the entire value from barang_qty .

and then this 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 for getting the value of my model is total_item .

and then this is my view 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 show the value of my request in Line D . When I try to call a variable with

<?php echo $total_item ?>

      

but doesn't work. Where am I wrong? please advise, thanks :)

+3


source to share


6 answers


The function of your model should be like this. there is an active write method to select the sum of any column



function counterItem(){
    $this->db->select_sum('barang_qty');//standard codeigniter method for getting sum
    $t= $this->db->get('barang')->row();
    return $t->barang_qty;//gives Sum of barang_qty columns value
}

      

+1


source


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



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

+2


source


Change your model function as follows

function counterItem()
{
  $query = $this->db->query("SELECT SUM(barang_qty) as sum FROM barang");
  return $query->row()->sum;
}

      

0


source


Maybe you can try this in your model:

public function counterItem(){
  $this->db->select('barang_qty');
  $this->db->from('barang');
  $this->db->where(); // if you want to put some condition
  $query = $this->db->get();

  return $query->result_array();
}

      

Your controller already looks fine,

Change view:

    <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 }
     }
  ?>

      

0


source


In the model

function counterItem()
{
   $query = $this->db->query('SELECT SUM(barang_qty) FROM barang');//Query to get data from database
   $result = $query->result_array();//assign $query to array 
   $count = count($result);//counting number of elements in $result array
   return $count;// return number of count
}

      

In the controller

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');
}

      

In sight

<?php echo $total_item?>

      

0


source


Thanks to Portfolio Mayanca. IT works with this Model function:

    function counterItem(){
    $this->db->select_sum('barang_qty');
    $t = $this->db->get('barang')->row();
    return $t->barang_qty;
}

      

And thank you all, thank you for your attention :) considering

0


source







All Articles