我在codeigniter中有一個預訂模塊,其中我限制用戶每天僅預訂會所2小時。我目前正在codeigniter中創建一個驗證來限制他們的預訂。我所做的是選擇所有預訂,並將它們按具有相同日期的行進行分組,以正確限制它們。問題是我創建的模型只返回1行,而不是所有的結果。這意味着我的計數器只被一行更改,我希望所有行都應該影響計數器。 下面是我的數據庫表:Codeigniter Group通過僅返回第一行
基本上是不應該在第二行被插入到數據庫中,因爲用戶「20130123」已經用盡了他的最大預訂當天這兩個小時。我在下面提供了驗證檢查,檢查用戶是否已經用完了兩個小時的預訂,而我的邏輯是我只是在預訂開始時減去預訂結束。使用上面的表格作爲例子和我的解釋,我的問題是,在我的模型中,計數器的值變成只有「2」,因爲它只讀取第一行(8 - 6 = 2),而不是「3」(結果第一行是2,然後加上第二行的結果是1:[(8-6)+(9-8)])
總結起來,我的問題在於計數器的值,因爲它只被查詢讀取的第一行添加。
這是我的代碼。
型號:
function check_twohours_clubhouse()
{
$query = $this->db->select('*')->from('clubhouse_reservation')->where('username', $this->session->userdata('username'))->group_by('reservation_date')->having('count(reservation_date) > 1')->get();
$result = $query->result();
if($query->num_rows() > 0)
{
$ctr = '0';
foreach($result as $row)
{
$totalhour = $row->reservation_end - $row->reservation_start; // subtract reservation start to reservation end to get the number of hours
$ctr = $ctr + $totalhour;
}
$ctr = $ctr + ($this->input->post('reserveend') - $this->input->post('reservestart')); // add the selected "add reservation" hours to the counter
if($ctr > 2) // counter > 2 hours limit
{
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return TRUE;
}
}
控制器:
function create_reservation_clubhouse()
{
$this->form_validation->set_error_delimiters('<div class="error">','</div>');
$this->form_validation->set_rules('datepick', 'Date', 'required|no_olddate');
$this->form_validation->set_rules('reservestart', 'Reservation Start', 'required|hourselection|unique_reserve_clubhouse|max_twohours');
if ($this->form_validation->run() == FALSE)
{
$this->template->load('user_template', 'view_userreservation_addclubhouse');
}
else
{
if($this->model_reservation_user->check_twohours_courtone())
{
if($query = $this->model_reservation_user->create_reservation_clubhouse())
{
$this->session->set_flashdata('reservefeedback', 'You have successfully reserved a date for the Clubhouse. Please wait for the administrators to accept your reservation.');
redirect('user_reservation/clubhouse');
}
}
else
{
$this->session->set_flashdata('reservefail', 'You cannot reserve more than two hours of Clubhouse per day.');
redirect('user_reservation/clubhouse');
}
}
}
請提供一些示例數據,提供CI從代碼中生成的SQL查詢,查詢返回的數據以及希望查詢根據示例數據返回的數據。 – Shadow
我在數據庫表中添加了一個解釋。謝謝 – coderszx