我一直在一个网站上工作,我需要从特定的开始日期到特定的结束日期迭代数周,并对该范围的列求和,然后比较它并返回最大值。我正在总结一列并尝试将最大值返回给我的控制器。这是我的模型代码:
简而言之,我试图:
将特定周范围内的所有销售额相加,然后返回最高一周的销售额。
function get_best_week_in_range($rep_id, $start_date, $end_date)
{
$highest_total = 0;
$date = $start_date;
while($date < $end_date)
{
$this->db->select('u.rep_id, s.sales_quantity, sum(s.sales_quantity) as sales_quantity ');
$this->db->join('sales as s','s.sale_rep_id = u.rep_id');
$this->db->where('s.date >=', $start_date);
$this->db->where('s.date <=', $end_date);
$this->db->group_by('u.rep_id');
$this->db->order_by('sales_quantity', 'desc');
$query = $this->db->get('users as u');
$row = $query->row();
$highest_total = ($row->sales_quantity > $highest_total) ? $row->sales_quantity : $highest_total;
$date = strtotime("+1 week", $date);
}
return $highest_total;
}
此代码返回“0”作为最高总数。如果有人能启发我,我将不胜感激。
感谢您的任何帮助!