SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Location
    spain
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get all values of an array to match

    Hello Again,

    Case :
    php booking calendar with db
    certain days can't be booked seperatly and needed to be booked regarding
    the set "block".
    For ex. 01-04-2008 and 02-04-2008 need to be booked together.

    So if only 1 of them is selected by client between his arrival date and leaving
    date, error message shows up.
    If both days are selected in clients staying dates array, the booking is ok.

    I got the script working that if 1 of both days are selected, an error message shows up, but I'm struggling if all blocked days are selected the booking can
    continue.

    Here is what i got so far:

    db : table 'dates' with 'blok' column, if value = "0", there is no block, if value ="1" there is a block on that day.

    First
    i get through the form the $arrival date and $end date,
    my script get all days in between and are put together in array $Days[].

    Then i select all rows in db where value =1

    $result=mysql_query("SELECT * FROM $table1 WHERE block =1") or die (mysql_error());
    while($row=mysql_fetch_array($result))
    {$date=$row['date'];

    //Then during this while loop i check every value in my $Days array if they match.

    foreach( $Days as $key => $value)
    {
    if ($value == $date)
    {$block=$date;}

    }
    }
    //finaly the error message is printed
    echo $block."is being blocked";

    This works fine...however, how to : if all blocked days are selected by client the booking can continue?

    I apreciate any help.

    Many thanks.

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $days_sql "('" implode("','"$days) . "')";
    $result mysql_query("select count(*) as blocked from $table1 where block = 1 and `date` in $days_sql");
    $row mysql_fetch_assoc($result);
    if (
    $row['blocked'] == count($days)) {
      echo 
    "yay!";
    } else {
      echo 
    "boo!";

    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Location
    spain
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Longneck,

    thank you for your answer. It took me a while to reply because first i wanted to examinate your code and to find out what you were doing ..
    Some code you used was new to me, and i went and search to find the meaning of it.
    I didn't want just copy and paiste your code, so i'll never learn.

    However after understanding what you are doing and testing, i must say
    to my humble opinion the script doesn't match 100%.

    the case was :
    some days are blocked in order to be booked only together.
    casestudy : 2008-04-01,2008-04-02,2008-03 are set to be blocked in database.
    As soon the 3 dates are booked together, the booking can continue, only
    if 1 or 2 out of the 3 are chosen, we get an error message.

    Testing learned me:
    Test 1:
    starting date: 2008-03-30
    end date: 2008-04-05
    (for easyning things out i give the echo results of my test):
    //echo all days of array $Days ( the array containing the days of the client)
    startdate: 2008-03-30
    nextday: 2008-03-31
    nextday: 2008-04-01
    nextday: 2008-04-02
    nextday: 2008-04-03
    nextday: 2008-04-04
    nextday: 2008-04-05
    enddate: 2008-04-05
    //echo all days that are blocked in this array through my script posted in my first post
    2008-04-01 being blocked.'
    '2008-04-02 being blocked.'
    '2008-04-03 being blocked.'
    //echo $days_sql
    '$days_sql= ('2008-03-30','2008-03-31','2008-04-01','2008-04-02','2008-04-03','2008-04-04','2008-04-05')
    //echo $row['blocked']
    row blocked: 3
    //echo final result
    boo!

    Test2:
    startdate: 2008-04-01
    nextday: 2008-04-02
    enddate : 2008-04-03


    2008-04-01 being blocked.'
    '2008-04-02 being blocked.'
    '2008-04-03 being blocked.'
    '$days_sql= ('2008-04-01','2008-04-02','2008-04-03')
    row blocked: 3
    yay!

    So in both case all 3 days are selected to be booked, but the 2 tests are giving another result.

    Much apreciated to have a look.

    all my respect.

  4. #4
    SitePoint Member
    Join Date
    Jun 2008
    Location
    spain
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to Longneck who putted me on the right track i found a solution.

    However it requires lots of querries...and an unique blocking number for each
    set of dates that are being blocked.

    if somebody knows an easier way of doing it please let me know.

    (If my solution could be at any assistence, please let me know, and i upload it)

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i still don't understand what you're trying to do, but if you post the code that works, i'll see if i can simplify it.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  6. #6
    SitePoint Member
    Join Date
    Jun 2008
    Location
    spain
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Longneck,

    much apreciated you want to dig deeper into this topic ...

    As i read it is not so clear to you what i want to achieve, i try to explain it as fine as
    i can:
    guesthouse booking online.
    With a availibility calendar.
    the client can ofcourse only reserve available days.
    But some days come in a "package", meaning if you want to reserve 1 of those days,
    you have to reserve the whole package (for ex. all week-end)
    Meaning : you can't book just saturday or just sunday, or friday+saturday, or sunday+monday...you need make a booking wherein at least saturday+sunday is included: friday only or friday+saturday+sunday or sunday+monday+... etc..
    Hope this made it a little bit clearer.

    Here's my code, which works fine, however to my opinion is quite complex because of
    all those querries.

    //
    We start from situation that every block has an unique number
    for ex.: weekend july : 05-07-2008 and 06-07-2008 has blocknumber 1
    weekend august : 02-08-2008 and 03-08-2008 has blocknumber 2
    unblocked days have number 0

    //db table contains column: fecha2 (for the dates) and bloque_SR(for the nrs of the blocks)

    // we get the variables $arrival and $leaving through the bookingform, supplied by client on guesthouse website.

    $startdate=$arrival;
    $enddate=$leaving;
    //adapting it to format db
    $start =date("Y-m-d", strtotime($startdate));
    $end=date("Y-m-d",strtotime($enddate));

    //setting arrays
    $aDays[] = $start;
    //get all days between $start and $end
    $current = $start;
    while($current < $end)
    {
    // Add a day to the current date
    $current = date("Y-m-d", strtotime("+1 day", strtotime($current)));
    // Add this new day to the aDays array
    $aDays[] = $current;
    }
    //select all blocked days from database
    $result=mysql_query("SELECT * FROM $table1 WHERE bloque_SR != '0' ") or die (mysql_error());
    //find out which days selected by client are being blocked
    while($row=mysql_fetch_array($result))
    {
    $fecha=$row['fecha2'];

    foreach ( $aDays as $key => $value)
    {
    switch ($value)
    {case "$fecha" :
    $block=$value;

    //add blocked days selected by client in array $aBlock
    $aBlock[] = $block ;
    break;}
    }

    }
    //find out if all blocked days are being selected by client
    $block_sql="('" . implode("','", $aBlock) . "')";

    $result1 = mysql_query("select * from $table1 where bloque_SR != '0' and `fecha2` in $block_sql");
    //getting the correct blocking number of the blocked days selected by client
    $row1 = mysql_fetch_array($result1);
    $num1=$row1['bloque_SR'];

    //find out how many days are having the same blocking number as chosen by client
    $result2 = mysql_query("select * from $table1 where bloque_SR = '$num1' ");
    while($row2 = mysql_fetch_array($result2))
    {$num2=$row2['bloque_SR'];
    //adding all days in array $aNum
    $aNum[]= $num2;
    }

    //check if the blocked days selected by the client are the same amount as the block.
    if (count($aBlock)==count($aNum))
    {$check =="ok";}
    else {
    $check =="error";
    echo "You have selected a date that's part of a block. Please select all days of this block.<br>";
    }

    As you can see lots of querries...
    If somebody can simplify this, it would be much apreciated!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •