Tính tổng của cột trong php codeigniter
Tôi đang cố gắng tính tổng số tiền trong bảng cơ sở dữ liệu. Bảng được gọi là tbl_payment. Thông tin được hiển thị trong bảng điều khiển quản trị Show
Đây là phần từ tệp view_Dashboard
Đây là tệp Bộ điều khiển - Bảng điều khiển. php php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('admin/Model_common');
$this->load->model('admin/Model_dashboard');
}
public function index()
{
$data['setting'] = $this->Model_common->get_setting_data();
$data['total_category'] = $this->Model_dashboard->show_total_category();
$data['total_news'] = $this->Model_dashboard->show_total_news();
$data['total_destination'] = $this->Model_dashboard->show_total_destination();
$data['total_tour'] = $this->Model_dashboard->show_total_tour();
$data['total_team_member'] = $this->Model_dashboard->show_total_team_member();
$data['total_client'] = $this->Model_dashboard->show_total_client();
$data['total_service'] = $this->Model_dashboard->show_total_service();
$data['total_testimonial'] = $this->Model_dashboard->show_total_testimonial();
$data['total_traveller'] = $this->Model_dashboard->show_total_traveller();
$data['total_vehicle'] = $this->Model_dashboard->show_total_vehicle();
$data['paid_amount'] = $this->Model_dashboard->show_total_paid_amount();
$this->load->view('admin/view_header',$data);
$this->load->view('admin/view_dashboard',$data);
$this->load->view('admin/view_footer');
}
} Và cuối cùng là file Model - Model_dashboard. php php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_dashboard extends CI_Model
{
public function show_total_category()
{
$sql = 'SELECT * from tbl_category';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_news()
{
$sql = 'SELECT * FROM tbl_news';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_destination()
{
$sql = 'SELECT * from tbl_destination';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_tour()
{
$sql = 'SELECT * from tbl_tour';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_team_member()
{
$sql = 'SELECT * from tbl_team_member';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_client()
{
$sql = 'SELECT * from tbl_client';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_service()
{
$sql = 'SELECT * from tbl_service';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_testimonial()
{
$sql = 'SELECT * from tbl_testimonial';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_traveller()
{
$sql = 'SELECT * from tbl_traveller';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_vehicle()
{
$sql = 'SELECT * from tbl_vehicle';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function show_total_paid_amount()
{
$sql = 'SELECT `p_id`, SUM(`paid_amount`) AS `total_paid_amount` FROM `tbl_payment` GROUP BY `p_id`';
$query = $this->db->query($sql);
return $query->num_rows();
}
} Tôi đã thử tất cả những gì tôi có thể tìm thấy. Tôi có 1 bản ghi trong Cơ sở dữ liệu hiển thị số tiền là $360. 00. Tuy nhiên, kết quả trong Dashboard chỉ hiển thị số 1 Mong các bạn giúp đỡ Chúc mừng, Đan
Liên kết để bình luậnChia sẻ trên các trang web khácNhiều tùy chọn chia sẻ hơn
đáĐăng ngày 24 tháng 9 đá
Đăng ngày 24 tháng 9
Bởi vì bạn đang yêu cầu số lượng hàng, không phải dữ liệu thực tế
Bạn muốn tìm nạp dữ liệu hàng và sử dụng dữ liệu đó. Một số tìm kiếm gợi ý rằng bạn nên làm điều đó bằng cách sử dụng mã này return $query->row()->total_paid_amount Ngoài ra, nếu bạn đang cố lấy tổng của tất cả các khoản thanh toán, có thể bạn không muốn sử dụng NHÓM THEO và nên xóa p_id khỏi danh sách đã chọn của mình Làm cách nào để tìm tổng của một cột trong PHP?Làm cách nào để lặp lại tổng trong PHP?$sum = $number1+$number2; echo "Tổng của $number1 và $number2 là. ". $ tổng;
Làm cách nào để tính tổng nhiều cột trong MySQL?Ví dụ. Hàm MySQL SUM() sử dụng nhiều cột
. Câu lệnh MySQL ở trên trả về tổng của phép nhân 'receive_qty' và 'purch_price' từ bảng mua hàng cho từng nhóm danh mục ('cate_id'). MySQL SUM() function retrieves the sum value of an expression which is made up of more than one columns. The above MySQL statement returns the sum of multiplication of 'receive_qty' and 'purch_price' from purchase table for each group of category ('cate_id') .
Làm thế nào để sử dụng tổng () trong MySQL?Hàm sum() của MySQL được sử dụng để trả về tổng giá trị tổng của một biểu thức. Nó trả về NULL nếu tập kết quả không có hàng nào. Nó là một trong những loại hàm tổng hợp trong MySQL. . CHỌN TỔNG (biểu_thức tổng hợp) TỪ bảng [điều kiện NƠI]; |