-5

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?

yivi
  • 42,438
  • 18
  • 116
  • 138
Sthurley
  • 5
  • 4
  • _“How can i get around this?”_ – by checking if variables you want to access exist before doing so. isset/empty. – CBroe Mar 01 '17 at 09:28
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Epodax Mar 01 '17 at 09:29
  • by checking if `$message` isset and is not empty, before using it – Masivuye Cokile Mar 01 '17 at 09:47

2 Answers2

-1

Use isset/empty

if(isset($var1) || !empty($var1)){
    //do something
} else {
    //do another
}    
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
  • I am not sure what you wanted to achieve with empty(), but read about how empty() works, and what does it return for different types. http://php.net/manual/pl/function.empty.php – jakub wrona Mar 01 '17 at 09:37
  • That should be `!empty`. I use this after form submission for checking, but I rarely use it. Most of the time, `isset()` is enough for me – Carl Binalla Mar 01 '17 at 09:40
  • @Downvoter Hope you won't leave without leaving a reason for the downvote – Carl Binalla Mar 01 '17 at 09:41
-1

Switching off notices, warnings, errors is not the best way to code. And unlike the above answers I prefer to always initialize a variable rather then using isset().

jakub wrona
  • 2,212
  • 17
  • 17