特定の開始日から特定の終了日まで週単位で繰り返し、その範囲の列を合計し、それを比較して最高値を返す必要があるサイトで作業しています。列を合計して、コントローラーに最大値を返そうとしています。ここに私のモデルコードがあります:
簡単に言えば、私はしようとしています:
特定の週の範囲のすべての売上を合計し、最も高い週の売上を返します。
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;
}
このコードは、highest_total として「0」を返しています。誰かが私を啓発できるなら、私はそれを感謝します。
助けてくれてありがとう!