0

UPDATE

I edited my code, so now the time is checked correctly:

while($row = mysqli_fetch_array($result)){
  $rid = $row['roomid'];
  $begin = $row['start'];
  $bval = strtotime($begin);
  $einde = $row['end'];
  $eval = strtotime($einde);

  $staco = strtotime($start); //starttime posted from form
  $endco = strtotime($end);  //stoptime posted from form


  $abegin = array(sta => $bval);
  $aeinde = array(sto => $eval);
  // print_r($abegin);
  // print_r($aeinde);
  foreach($aeinde as $sto) {
  if($staco <= $sto) {$checksto = 1;}
  else {$checksto = 0;}
  }

  foreach($abegin as $sta) {
  if($endco <= $sta) {$checksta = 1;}
  else {$checksta = 0;}
  }


  if($checksta == $checksto) {$ok = 1;} else {$ok = 0;}  

  print_r($ok);

  }
  }
}

So now: how do I check if $ok contains one or more 0's (don't book the room) or all 1's (book the room).

$roomstate = array(state => $ok) results in more than one array:

Array ( [state] => 0 ) Array ( [state] => 1 ) Array ( [state] => 1 )

I'm doing something wrong, because I think it should be possible to get all the different $ok's in one array and then

if(in_array(0,$roomstate)) { echo "Do not book";} else {$bookitsql = "INSERT INTO reservations ...";}
UPDATE: There is a flaw in my original logic to check availabilty with the rooms that needs to be solved first: now the rooms are not checked correct, so it is impossible to answer this question since the correct data is not displayed. My apologies.

For a system that books rooms I need to check with a new booking if the room is already is booked at the moment. The booking works with a form, and then it compares the results from the form with the content in the database for that room on that date.

    while($row = mysqli_fetch_array($result)){
      $rid = $row['roomid'];
      $begin = $row['start'];
      $bval = strtotime($begin);
      $staco = strtotime($start);
      $einde = $row['end'];
      $eval = strtotime($einde);
      $endco = strtotime($end);

      $abegin = array(sta => $bval);
      $aeinde = array(sto => $eval);

        foreach($abegin as $sta) {
        if($staco  $checksta,checkstop => $checksto);
          print_r($meh);
    }

BetterQuestion:

Now I get `$staco` and `$endco`, which are the start and stoptime from the form.
I also get `$sta` and `$sto`, which are multiple start and stoptimes from the database.

Example: 
existing reservations:

        sta     sto
    1:  0800    0959
    2:  1130    1259

So now when I get `$staco = 1000` and `$endco = 1114` it doesn't check right.
It only works if the new reservation is later than all the other reservations in the database. How can I solve this?
lennert_h
  • 113
  • 12
  • **Sidenote:** There is no more support for `mysql_*` functions, they are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation), no longer maintained and will be [removed](http://php.net/manual/en/function.mysql-connect.php#warning) in the future. You should update your code with [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) to ensure the functionality of your project in the future. – Amal Murali Jan 28 '14 at 13:40
  • To check all the values at once. Now it checks $checksta and $checksto for every row in the database (on that date and room). So now I sometimes get the result `Array ( [checkstart] => 1 [checkstop] => 0 ) Array ( [checkstart] => 0 [checkstop] => 0 )` so it must not add the reservation. But because of the second array it will think it's allright to book the room anyway – lennert_h Jan 28 '14 at 13:45
  • @AmalMurali my sql statements now work with mysqli. – lennert_h Jan 28 '14 at 13:55

2 Answers2

0

Your Target Pseudocode shows that you want it all be 0. $meh is an Array. So what you do now is using a foreach on $meh and then Mark it as Occupied as soon as 1 appears. if this doesnt happen, you can assume that $meh is free, and do you Booking.

$roomState = 0;
foreach($meh as $mehValue){
  if($mehValue == 1){
    $roomState = 1;
  }
}
if($roomState == 1){
  print_r("room is occupied");
} else {
  //insert stuff
}
Panade
  • 309
  • 3
  • 12
  • This doesn't really work, since $meh is in fact multiple arrays. `print_r($meh)` give me likewise output: `Array ( [checkstart] => 1 [checkstop] => 1 ) Array ( [checkstart] => 0 [checkstop] => 0 )` (Depending on how much reservations are already in the database for that room on that date). – lennert_h Jan 28 '14 at 13:58
  • So if i get this Right. $meh is an Array with containing Arrays of these 2 "checkstart" and "checkstop" values? If so, you can either iterate trough those both too or you can also just do 2 Checks with the Keys if they are consistend. (hardcode variant) if( $mehValue[checkstart] || $mehValue[checkstop]){ its occupied} Should work because if its 1, its interpreted as true, if its 0, it should be interpreted as false. – Panade Jan 28 '14 at 14:04
  • Yes, you got it right, but I now see a mistake in my original logic, so this cannot work at all (my original code). The way of checking the availabilty with the time from the form is completely wrong. I will update if I have figured out how to do this correct. – lennert_h Jan 28 '14 at 14:23
  • @lennert_h You want to compare the Dates now. Assuming that you can compare the numbers (which is not the case if you have a leading 0) you need to find the Space between 2 Values: -- Highest STOP Time that is smaller than START Time from the Form // Lowest START Time that is higher than the STOP Time from the Form -- -- Mark those 2 Checks with an Boolean Value (true, false) and you can again Check if both Conditions meet. If this is solving your Problem, leave a Comment, so I can add it to the given Solution – Panade Jan 29 '14 at 12:39
  • Comparin the dates was always the plan, but I, wrongfully, thought I had that covered in my original code. Not really sure now, but thinking about it, how to compare the dates now. In the datetime format, I work with leading zeros, but they are converted to a timestamp. Is that sufficient? – lennert_h Jan 29 '14 at 14:04
  • this is usefull. the timestamp is simply a counting number of secounds since it startet many many years ago. to find the gap and if its free, the conditions i wrote must be met. you could use min max informations if this helps. – Panade Jan 29 '14 at 15:11
  • I will see if can work with that. With min max informations, I guess you something like: http://stackoverflow.com/questions/5846156/get-min-and-max-value-in-php-array ? – lennert_h Jan 30 '14 at 08:57
  • As an example, yes. MySQL can also return Max Values if I remember right. What you want, is avoiding too much foreach and specialy nested foreach. Just imagine the Dates as a Timeline. What you wanna test is, if the given time frame (start to end) is not occupied by other timeframes (reservations). Then you look at the conditions for your start and the end times. Maybe its also important to crosscheck that there is no reserved start time inbetween your Timeframe. – Panade Jan 30 '14 at 10:10
  • I'try and read as much on the subject as I can. I already thought that my way of getting data from the database and creating arrays was a totally wrong approach, so I will return to the drawingboard :-) – lennert_h Jan 30 '14 at 10:50
  • > Maybe its also important to crosscheck that there is no reserved start time inbetween your Timeframe. That's why I checked all the times in the database `$endco <= $sta` and `$staco <= $sto`. – lennert_h Jan 30 '14 at 11:04
  • Basically, following pseudocode is what I need (If I understand correctly)?: `max($sto<$startco)=$1;min($sta>$endco)=$2;$range=$1-$2;$orig=$endco-$starco;id($orig>=$range){//book}else{//do not book};` – lennert_h Jan 30 '14 at 12:34
  • i mean it more like getting the max on a set of values. this is what i would compare to archieve the tests. – Panade Jan 30 '14 at 14:12
  • That was also what I meant, getting the max from every value that is lower than the starttime from the form. Not sure how to achieve this in real code though, at the moment. – lennert_h Jan 30 '14 at 14:17
  • Trying a different approach now, by looking directly in the database if a date already exists: ` $reservationsql = "SELECT * FROM reservations WHERE roomid = '$roomid' AND ('$start' between start AND end) OR ('$end' between start AND end) OR ('$start' >= start AND '$end' <= end) OR ('$start' <= start AND '$end' >= end)";` – lennert_h Jan 30 '14 at 17:36
0

Took a different approach, by checking date in my sql-statement. So, the form posts $start and $end (when the users wants to make a reservation). Now my code looks like this (and it looks ok, I think):

//form and other errormessages like username==0; $start>$end etcetera
else {
  $reservationsql = "SELECT * FROM reservations WHERE roomid = '$roomid' AND ('$start' between start AND end) OR ('$end' between start AND end) OR ('$start' >= start AND '$end' <= end) OR ('$start' <= start AND '$end' >= end)";
  $result = mysqli_query($link,$reservationsql) or die(mysql_error());
  $num = mysqli_num_rows($result);

  if($num>0) {"Error: room already booked at that moment";}
  else {//$query = "INSERT INTO ..."}
}
}mysqli_close();

Thanks for all the help here and to think along with me.

lennert_h
  • 113
  • 12