Codeigniter - Foreach只返回1行

问题描述:

对不起,如果这个问题可能很愚蠢,但我相对比较新的编程。我在我的网站上有一个预订功能,我创建了我自己的表单验证(MY_Form_validation),以检查是否在一天中的特定时间段内进行了插入。用户选择他们想要开始预定的时间,并且他们想要结束它的时间如下所示。 enter image description hereCodeigniter - Foreach只返回1行

基本上,保留是从早上10点到凌晨1点。在表格中,小时值如下: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; 
} 
+0

修复它现在,愚蠢的错误。我只是将查询结果限制为1行,并没有注意到它。多谢你们。 – coderszx

尝试此观看多行

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; 
} 
+0

我尝试用上面的代码替换我的代码,并且它仍然通过:/ – coderszx

试试这个方法:

$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; 
} 
} 
+0

尝试了上面的代码,并保留仍然推进:/ – coderszx

1)您只需要检查与设置您的查询结果。需要检查result_array()返回的结果集查询究竟返回了什么(它是返回所有行还是只返回1行)。如果可以有更多关于查询的细节,只需在查询语句之后放置下面的行。

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

它会给你确切的查询。通过查询,您可以了解出错的地方。

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

+0

SELECT * FROM'clubhouse_reservation' WHERE'reservation_date' = '02/24/2017' AND'reservation_status' = 1 这是什么显示 – coderszx

+0

是的,所以只是检查是你的查询返回所有行?你可以用$ checkresult-> result_array()来检查它。 。如果它返回所有行需要检查循环。 –