【发布时间】:2017-07-09 14:54:51
【问题描述】:
我在 codeigniter 中有一个预订模块,其中我限制用户每天仅预订 2 小时的俱乐部会所。我目前正在 codeigniter 中创建验证以限制他们的预订。我所做的是选择所有预订,并按具有相同日期的行对它们进行分组,以便适当地限制它们。问题是我创建的模型只返回 1 行,而不是所有结果。这意味着我的计数器只被更改了一行,我希望所有行都应该影响计数器。 下面是我的数据库表:
基本上,第二行不应该插入到数据库中,因为用户“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查询,查询返回的数据以及您希望查询根据您的示例数据返回的数据。
-
我在上面用数据库表添加了一个解释。谢谢
标签: php mysql codeigniter