Codeigniter - Foreach仅返回1行

时间:2017-02-24 10:50:34

标签: php arrays codeigniter foreach

抱歉,如果这个问题可能很愚蠢,但我对编程比较陌生。我在我的网站上有一个预订功能,我创建了自己的表单验证(MY_Form_validation)来检查是否已经在当天的特定时段使用了一个插槽。用户选择他们想要开始预订的小时以及他们想要结束的小时,如下所示。enter image description here

基本上,预订时间为上午10:00至凌晨1:00。在表格中,小时的值如下:10:00 ='10',11:00 ='11',12:00 ='12',1:00 ='13',2:00 =' 14'... 1:00 = '25'

enter image description here 我创建了一个数组,只要保留小时,它就会用作交换机。我所做的是创建一个foreach来获取具有特定日期的所有行,并创建一个“循环”数组的while循环,以模拟一小时的获取时隙。发生的事情是foreach循环只读取第一行,因此数组只被第一行而不是其他行修改。

示例:(参考上面的sql表) 如果我保留起始值为17且结束值为19的小时数,那么验证工作并返回“时间表已被预订”消息,因为它是第一行。但是,如果我保留起始值为11且结束值为14(第二行)的小时数,则验证不起作用,并且保留推送。 我该如何正确地做到这一点?非常感谢!

表格验证功能:

function unique_reserve_clubhouse()
{
    $reservedate = $this->CI->input->post('datepick');
    $reservestart = $this->CI->input->post('reservestart');
    $reserveend = $this->CI->input->post('reserveend');

    $checkstart = $this->CI->db->get_where('clubhouse_reservation', array('reservation_date' => $reservedate, 'reservation_start' => $reservestart, 'reservation_status' => 1), 1);

    $checkresult = $this->CI->db->get_where('clubhouse_reservation', array('reservation_date' => $reservedate, 'reservation_status' => 1));
    $resultreserve = $checkresult->result();
    $tdX = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

    foreach($resultreserve as $result)
    {
        while($result->reservation_start < $result->reservation_end)
        {
            $tdX[$result->reservation_start] = 1;
            $result->reservation_start++;
        }
    }

    if($checkstart->num_rows() > 0 || $tdX[$reservestart] == 1 || $tdX[$reserveend] == 1) {

        $this->set_message('unique_reserve_clubhouse', 'This time schedule is already booked.');

        return FALSE;
    }

    return TRUE;
}

3 个答案:

答案 0 :(得分:0)

尝试使用此功能查看多行

function unique_reserve_clubhouse()
{
$reservedate = $this->CI->input->post('datepick');
$reservestart = $this->CI->input->post('reservestart');
$reserveend = $this->CI->input->post('reserveend');

$checkstart = $this->CI->db->get_where('clubhouse_reservation', array('reservation_date' => $reservedate, 'reservation_start' => $reservestart, 'reservation_status' => 1), 1);

$checkresult = $this->CI->db->get_where('clubhouse_reservation', array('reservation_date' => $reservedate, 'reservation_status' => 1), 1);
$resultreserve = $checkresult->result();
$tdX = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

foreach($resultreserve as $key => $value)
{
   echo $key.''.$value; //Here i have put echo so that you can check it here only.
}

if($checkstart->num_rows() > 0 || $tdX[$reservestart] == 1 || $tdX[$reserveend] == 1) {

    $this->set_message('unique_reserve_clubhouse', 'This time schedule is already booked.');

    return FALSE;
}

return TRUE;
}

答案 1 :(得分:0)

试试这个方法:

$condition = "reservation_date ='" .$reservedate . "'";
$this -> db -> select('*');
$this -> db -> from('clubhouse_reservation');
$this -> db -> where($condition);
foreach( $query -> result() as $record){
 if($record->reservation_start >= $reservestart && $record->reservation_end <= reserveend){
    //Has someone reserved      
    break;
 }
}

答案 2 :(得分:0)

1)您可以查看您的查询结果集吗?需要使用result_array()检查结果集查询究竟返回的内容(是返回所有行还是仅返回1行)。如果可以在查询语句之后放在下面的行中,可以获得有关查询的更多详细信息。

 echo $this->CI->last_query(); die;

它会为您提供准确的查询。通过查询,您可以了解出错的地方。

2)如果它返回所有行,那么你需要检查你的foreach循环和