Php 只返回第一行的Codeigniter组

Php 只返回第一行的Codeigniter组,php,mysql,codeigniter,Php,Mysql,Codeigniter,我在codeigniter中有一个预订模块,我限制用户每天只预订2小时的会所。我目前正在codeigniter中创建一个验证来限制他们的预订。我所做的是选择所有的预订,并按日期相同的行对它们进行分组,以便正确地限制它们。问题是我创建的模型只返回1行,而不是所有结果。这意味着我的计数器只改变了一行,我希望所有行都会影响计数器。 下面是我的数据库表: 基本上,第二行不应该插入数据库,因为用户“20130123”已经用完了当天的最大预订量,即两小时。我在下面提供了验证检查,检查用户是否已使用了两个小

我在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');
        }
    }
}
创建预订俱乐部()功能
{
$this->form_validation->set_error_分隔符(“”,);
$this->form_validation->set_规则('datepick','Date','required','no_olddate');
$this->form_validation->set_规则('reservestart','reservestart','required | hourselection | unique | reserve | clubhouse | max | twooh');
如果($this->form\u validation->run()==FALSE)
{
$this->template->load('user_template','view_userreservation_addclubhouse');
}
其他的
{
如果($this->model\u reservation\u user->check\u twoohours\u courtone())
{
如果($query=$this->model_reservation_user->create_reservation_clubhouse())
{
$this->session->set_flashdata('reservefeedback','您已成功为俱乐部预订了日期。请等待管理员接受您的预订');
重定向(“用户预订/会所”);
}
}
其他的
{
$this->session->set_flashdata('reservefail','每天不能预订超过两小时的会所');
重定向(“用户预订/会所”);
}
}
}

您可以使用和函数在sql中计算所有这些:

select reservation_date, sum(time_to_sec(timediff(reserveend, reservestart)) / 3600 as reserved_hours
from clubhouse_reservation
where username =...
group by reservation_date
having count(*)>1 --although it is not clear to me why you have this restriction
上述查询将汇总每个预订日期的给定用户预订会所的小时数(3600=一小时内的秒数)

在php代码中,您只需检查结果集中的
保留时间是否大于2


我不是一个真正的CI专家,因此我不知道如何将上面的sql转换为CI。但我担心您必须使用原始sql,因为它计算时间的方式。

您可以使用和函数在sql中计算所有这些:

select reservation_date, sum(time_to_sec(timediff(reserveend, reservestart)) / 3600 as reserved_hours
from clubhouse_reservation
where username =...
group by reservation_date
having count(*)>1 --although it is not clear to me why you have this restriction
上述查询将汇总每个预订日期的给定用户预订会所的小时数(3600=一小时内的秒数)

在php代码中,您只需检查结果集中的
保留时间是否大于2


我不是一个真正的CI专家,因此我不知道如何将上面的sql转换为CI。但我担心您必须使用原始sql,因为它计算时间的方式。

请包含一些示例数据,提供CI根据代码生成的sql查询、查询返回的数据以及您希望查询基于示例数据返回的数据。我在上面的数据库表中添加了一个解释。Thankspl包含一些示例数据,提供CI根据代码生成的SQL查询、查询返回的数据以及您希望查询根据示例数据返回的数据。我在上面的数据库表中添加了一个解释。谢谢,我还没有试过,但是由于我的reserveend和reservestart值只是整数,timediff会工作吗?我还没有试过,但是我的reserveend和reservestart值只是整数,timediff会工作吗?