I would like to sum a column of a database with certain WHERE criteria then display it on the webpage, but am unsure of how to do so.
Here is what I have so far for my function in my controller.
public function get_hours() {
$user_email = $this->session->userdata('USER_EMAIL');
$this->db->where('USER_EMAIL', $user_email);
$this->db->select_sum('USER_WORK_HOURS');
$data = $this->db->get('user_hours');
$hours = $data->row()->USER_WORK_HOURS;
echo $hours;
}
This will take me to a blank page and not display anything.
I have also tried to echo out $data, but get an error saying it could not convert it to string.
The column that I am trying to sum is a TIME type column.
Try this:
public function get_hours()
{
$user_email = $this->session->userdata('USER_EMAIL');
$this->db->select('SEC_TO_TIME( SUM( TIME_TO_SEC( `USER_WORK_HOURS` ) ) ) AS totalHours');
$this->db->where('USER_EMAIL', $user_email);
$hours= $this->db->get('user_hours')->row()->totalHours;
echo $hours;
}