How to display total in view page (Codeigniter) - php

I am just new to Codeigniter. I want to display the total sum of bill_rpt of this SQL query in the view page. here's the code
this is my Model
public function total() {
$sql="SELECT bill_rpt
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
this is my Controller
public function payments($token=''){
$this->isLoggedIn();
$data = array(
// get data using email
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total($this->session->userdata('email'))->result_array(),
);
if ($this->session->userdata('position_id') == 2) {
$this->load->view('includes/admin_header',$data);
$this->load->view('admin/reports/payments',$data);
} else {
$this->logout();
}
}
this is my View
<h4 class="pull-right">Total:</h4>
Thanks in advance

Hope this will help you :
Use ci select_sum() to get the sum
Your total method should be like this :
public function total()
{
$this->db->select_sum('bill_rpt');
$this->db->where('bill_status' , '1');
$query = $this->db->get('amilyar_bill');
return $query->row()->bill_rpt;
}
Your $data part in controller payments method should be like this :
$data = array(
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total()
);
In payments view part use $total like this :
<h4 class="pull-right">Total: <?=$total;?></h4>

You can use num_rows() method of Active Class.
$query->num_rows();
public function total(){
$sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
$query = $this->db->query($sql);
return $query->num_rows();
}
And in View,
<h4 class="pull-right">Total: <?php echo $total;?></h4>

Try SUM function of MySql as:
In Model
public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}

Related

how can I get price cart from database in Code Igniter

it's my controller
function set_hargabesar($id){
$this->load->library('cart');
$condition['id'] = 'id';
$get = $this->myigniter_model->getharga('harga_satuan','id');
$data = array(
'rowid' => $id,
'qty' => 5,
'price' => $get,
);
$this->cart->update_all($data);
}
it's my model
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
}
How can I get a 'harga_satuan' (price cart) from database
please help me.
I can get the data(harga_satuan) from database with this code.
You've only executed your query, you need to retrieve data.
Model:
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
$row = $query->row_array();
return $row;
}
In your controller just change this:
'price' => $get['harga_satuan'],
And, of course, you need to pass id to your model:
$get = $this->myigniter_model->getharga('harga_satuan', $id);
(but I hope you are doing it)

database value how to pass dropdown list in codeigniter

How to pass value to drop down list in CodeIgniter?
This is my view file HTML code:
<div class="form-group">
<select name="department" id ="department">
<?php
foreach($dept as $country)
{
echo '<option value="'.$country['dept_id'].'">'.$country['managers_name'].'</option>';
}
?>
</select>
</div>
This is my controller code:
public function department()
{
$this->load->model('insert_model');
$data['dept'] = $this->insert_model->category_name_get();
}
This is my model file code:
function category_name_get()
{
$query = $this->db->get('dept');
if ($query->num_rows >= 1)
{
foreach($query->result_array() as $row)
{
$data[$row['dept_id']]=$row['managers_name'];
}
return $data;
}
}
I think you are searching for Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
In your case $data contains the department list in it as an array.
In Your COntroller file you will get all date on $data['dept'] that returned from model.
public function department()
{
$this->load->model('insert_model');
$data['dept'] = $this->insert_model->category_name_get();
$this->load->view('view_file_name',$data);
}
In your view file you will get this data
Just do print_r($dept); and check array..
you will find more info from this link
Probably your model is not returning "proper" data for your view. Perhaps something like this:
function category_name_get()
{
$data = array();
$query = $this->db->get('dept');
if ($query->num_rows >= 1)
{
foreach($query->result_array() as $row)
{
$data[] = array(
'dept_id' => $row['dept_id'],
'managers_name' => $row['managers_name']
);
}
}
return $data;
}
function category_name_get()
{
$query = $this->db->get('dept');
if ($query->num_rows() >= 1)
{
foreach($query->result_array() as $row)
{
$data[$row['dept_id']]=$row['managers_name'];
}
return $data;
}
}
Use num_rows() instead of num_rows. I hope it works now.

Codeigniter missing argument

Help guys.
This my error problem error:
I Think i follow right tutorial, but i don't know what the wrong from my code
there is my controllers
public function edit($id){
$where = array('id' => $id);
$data['barang'] = $this->model_barang->edit($where,'barang')->results();
$this->load->view('edit',$data);
}
public function update(){
$id = $this->input->post('id');
$jenis = $this->input->post('jenis');
$nama = $this->input->post('nama');
$harga = $this->input->post('harga');
$pemasok = $this->input->post('pemasok');
$data = array (
'jenis' => $jenis,
'nama'=> $nama,
'harga' => $harga,
'pemasok' => $pemasok
);
$where = array(
'id' => $id
);
$this->model_barang->update($where,$data,'barang');
redirect('barang/tampil');
}
and there is my model, i think my model is not showing any error
public function edit($where,$table) {
return $this->db->get($table,$where);
}
public function update($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
You need to pass the $id to the controller.
It should be something like this http://domain.com/barang/edit/1

how to do multiple search here the code

This is the code work fine but only for one column search which is user in below code, but need to search more than one column like password , email
controller
public function index()
{
$this->input->post('search'); //this is drag from view table *name=search*
$search=$this->input->post('search');
$data['user']=$this->usermodel->searchUser($search);
$this->load->view('user',$data);
}
model
public function searchUser($search=null)
{
$this->db->select("*");
$this->db->from("tbl_user");
$this->db->where("user",$search); //user is one column of table *tbl_user*
$query=$this->db->get();
if($query->num_rows()>0)
{
$result=$query->result_array();
return $result;
} else {
return false;
}
}
You can use multiple where like this
$this->db->where('email',$email);
$this->db->where('password',$password);
You can use an array and pass the array.
Associative array method:
$array = array('user' => $user, 'email' => $email, 'password' => $password);
$this->db->where($array);
// Produces: WHERE user = 'Joe' AND email = 'joe#example.com' AND password = 'password'
Please check CI Query Builder Class for details

How can i run a check on a MySQL database for a FB ID, & other personal data so only a certain page is shown when revisiting?

I have created a Facebook App that i need people to only enter their data to once.
It's all working and the database is being populated, but i need to make sure people don't keep coming back and re-entering their data endlessly.
What's the best way to check if the user has already submitted their data ?
The signed_request could still have been submitted and their data not entered so i need the check for both to work.
Ideally the PHP would just check for FB ID + other data, and only display a confirmation / thankyou page.
Currently my php to send to the database is:
class Users_Model extends CI_Model {
protected $_name = 'users';
function add($id,$key,$value) {
$data = array(
'id' => $id,
'name' => $key,
'value' => $value
);
return $this->db->insert($this->_name, $data);
}
function update($id,$key,$value) {
$data = array(
'value' => $value
);
$this->db->where(array(
'id' => $id,
'name' => $key
));
return $this->db->update($this->_name, $data);
}
function exists($id,$key=null) {
if($key == null) {
$this->db->where(array(
'id' => $id
));
} else {
$this->db->where(array(
'id' => $id,
'name' => $key
));
}
$query = $this->db->get($this->_name);
if($query->num_rows() > 0) {
return true;
}
return false;
}
function remove($id) {
$data = array(
'id' => $id,
);
return $this->db->delete($this->_name, $data);
}
function all() {
$query = $this->db->get($this->_name);
$results = array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$results[]=$row;
}
}
return $results;
}
}
Any help much appreciated...
What's the best way to check if the user has already submitted their data ?
Check if you already have a record for the user’s Facebook id in your database.

Categories