Codeigniter - Foreach仅返回1行

sorry if this question might be silly but I am relatively new to programming. I have a reservation function in my website and I created my own form validation (MY_Form_validation) to check whether a slot has been taken for a specific hour of the day. A user selects the hour they want to start the reservation and also the hour they want to end it as shown below.enter image description here

Basically, the reservations are from 10:00 am until 1:00 am in the morning. In the form, the values of the hours are as follows: 10:00 = '10', 11:00 = '11', 12:00 = '12', 1:00 = '13', 2:00 = '14' ... 1:00 = '25'

enter image description hereI created an array that will serve as switches whenever the hour has been reserved. What I did was create a foreach to get all the rows having the specific date, and create a while loop that 'switches' the arrays in order to simulate a taken slot for the hour. What happens is that the foreach loop only reads the first row, so the array is only being modified by the first row and not the other rows.

Example: (Referring to the sql table above) If I reserve for the hours having start value as 17 and end value as 19, then the validation works and it returns the "time schedule has already been booked" message, since it is the first row. However, if I reserve for the hours having the start value as 11 and end value as 14 (second row), the validation doesn't work, and the reservation pushes through. How do I properly do this? Thanks a lot!

Form Validation Function:

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

Try this for viewing multiple rows

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

Try this method :

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

1) Can you just check with your query results set. Need to check what exactly result set query is returning ( Is it returning all rows or only 1 row ) with result_array(). If can have more details about your query just putting below line after your query statement.

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

It will give you exact query. With Query you will get the idea where you going wrong.

2) If its return all rows then you need to check with your foreach loop and