For part of a project i am working on in school i am building a room booking system. As part of this system, i have a page where users can enter criteria for a room and the page will return available rooms that fit that criteria and are free for booking. If a users search does not return any results i intend to lower the criteria entered, display a room that fits the altered criteria and display a message to the user informing them of the altered criteria. The call to function suggestroom() is shown here.
} else {
    $reducecapacity = 1;
    do {
        $booking = new Booking();
        $suggestedrooms = $booking->suggestroom(($capacity - $reducecapacity), $appletv, $printer);
        $reducecapacity = $reducecapacity + 1;
    } while($suggestedrooms === null);
    echo 'This room has a cacpacity of: ' . ($capacity-($reducecapacity-1));
    for($x=0; $x<count($suggestedrooms); $x++) {
        echo $suggestedrooms[$x];
    }
}
Public function SuggestRoom($capacity, $appletv, $printer) {
    if($appletv == 1 and $printer ==0) {
        $roomname = DB::GetInstance()->query("SELECT roomname FROM room WHERE capacity >= '$capacity' AND appletv ='$appletv'");
    } elseif($appletv == 0 and $printer == 1) {
        $roomname = DB::GetInstance()->query("SELECT roomname FROM room WHERE capacity >= '$capacity' AND printer = '$printer'");
    } elseif($appletv == 1 and $printer == 1) {
        $roomname = DB::GetInstance()->query("SELECT roomname FROM room WHERE capacity >= '$capacity' AND appletv ='$appletv' AND printer = '$printer'");
    } else {
        $roomname = DB::GetInstance()->query("SELECT roomname FROM room WHERE capacity >= '$capacity'");
    }                   
    $roomcount = $roomname->count();
    if($roomcount == 0) {
        echo 'No classes match your criteria';
    } else {
        for($x=0; $x<$roomcount; $x++) {
            $RoomArray[$x] = $roomname->results()[$x]->roomname;            
        }
    }
    $LoopCount = 0;
    $EndLoop = false;
    $RNDnum = 0;
    $availableroomcount = 0;
    do {
         $suggestedRoom = $RoomArray[$RNDnum];
         $getRoomID = DB::GetInstance()->query("SELECT roomid FROM room WHERE roomname = '$suggestedRoom'");
         $roomid = $getRoomID->results()[0]->roomid;
         $bookingid =  Input::get('bookingdate') . Input::get('period') . $roomid;
         $CheckIfBooked = DB::GetInstance()->query("SELECT bookingid FROM booking WHERE bookingid = '$bookingid'");
         if($CheckIfBooked->count() ==0) {
            $availablerooms[$availableroomcount] = $suggestedRoom;
            $availableroomcount = $availableroomcount+1;
         }
         if($LoopCount===$roomcount-1) {
            $NoRoomMessage = true;
            $EndLoop = true;
            $suggestedRoom = null;
         }
         $LoopCount = $LoopCount+1;
         $RNDnum = $RNDnum +1;
    } while ($EndLoop <> 1);
   return $availablerooms;
}
Thus, when there are no bookings, a null array will be returned to suggested rooms and this will continue until a room is found (if not, i will make it so other criteria is changed, not that far ahead yet).
A room can be found, and the code works however for x amount of times that the code is ran before a room is found i.e an empty array is returned, i get an undefined variable message. How can i get around this?
 
     
     
    