Reputation: 77
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 :)
Upvotes: 3
Views: 6458
Reputation: 101
$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.
Upvotes: 2
Reputation: 344
Your Model function should be like this. there is active record method for selecting 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
}
Upvotes: 1
Reputation: 77
Thanks to Mayank Tailor. 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 thanks to all of you, thanks for your attention :) regards
Upvotes: 0
Reputation: 1139
Maybe you can try like 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 look okay ,,,
Edit your 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 }
}
?>
Upvotes: 0
Reputation: 38584
In 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 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 view
<?php echo $total_item?>
Upvotes: 0
Reputation: 16997
Modify your model function like this
function counterItem()
{
$query = $this->db->query("SELECT SUM(barang_qty) as sum FROM barang");
return $query->row()->sum;
}
Upvotes: 0