Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
SQL QUERY for checking reservation details
Old 05-19-2011, 02:16 PM SQL QUERY for checking reservation details
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
hello everyone, i have a php booking script i need to check availability of chapels depending on giving date and time here is the function:

PHP Code:
function CheckExistenceReservation($reserveDate,$StartTime,$EndTime,$MychapelID){ //2011-04-08
        
        
$sql_1="SELECT COUNT(res_date) AS MT FROM reservation WHERE res_date='$reserveDate' AND res_startTime='$StartTime' AND ('$EndTime'<= AND '$EndTime'>=res_startTime) AND  chap_id=$MychapelID";
        
$result=mysql_query($sql_1) or die (mysql_error());
        return 
mysql_result($result,0,'MT');
        
        } 
lets say i have a reservation 05/20/11 from 9:00 AM to 11:00 AM. if i try to make another reservation on 05/20/11 from 9:00 AM to whatever end time it wont let me, but if i try to make a reservation on 05/20/11 from 8:00 AM to 11:00AM its letting me do it and it should not happen because theres already a reservation from 9 to 11. i believe the function checking for the reservation is missing something please help.
stivens is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-19-2011, 03:37 PM Re: SQL QUERY for checking reservation details
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Note: This post turned out longer than I expected You can skip to the code part at the bottom if you'd like

Basically you just have to check if the start time or the end time is withing the range of another reservation. That is:

Lets call:

S = start time (from the database)
E = end time (from the database)
nS = new start time (the time you want to check)
nE = new end time (the time you want to check)

Now, if nS is between S and E (nS >= S AND nS <= E)

OR

nE is between S and E (nE >= S AND nE <= E) )

You have found a collision.

However, I think I've come up with a more effective way. I'll try to guide you through my thinking, step by step. Hopefully it will make sence.

Lets say we have two reservations, an existing one and a new one that we want to check if it collides.
Now lets say that we let our friend check for us, and he guarantees to 100% that the new one does not collide with the existing one.
That means we KNOW that the new one is either before or after the existing one.
That is, it either starts and ends before the existing one starts. Or, it starts after the existing one ends.
So with the names above, that means (nE <= S OR nS >= E).

Now we are able to check if it does not collide. But what you want to check is if it does collide. So we negate it with an exclamation mark (!).
if !(nE <= S OR nS >= E) is true, you found a collision.

Then there is actually a law for these kinds of logical expression. It may be tricky to really understand this last part, but that expression is the same as (nE > S AND nS < E). Which is a bit easier to write.

This check does require that nS < nE though. That is, that a reservation doesn't end before it starts. And that your fields are of a date type, so that you can compare dates with the < and > operation.

So, as a summary and the final awnser to your question: try this

PHP Code:
function CheckExistenceReservation($reserveDate,$StartTime,$EndTime,$MychapelID){ //2011-04-08 
         
   
$sql_1="SELECT COUNT(res_date) AS MT FROM reservation "
        
"WHERE res_date='$reserveDate' AND chap_id=$MychapelID "
        
"AND '$EndTime' > res_startTime AND '$StartTime' < res_endTime"
   
$result=mysql_query($sql_1) or die (mysql_error()); 
   return 
mysql_result($result,0,'MT');       

__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 05-19-2011 at 03:41 PM.. Reason: Clarification
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-19-2011, 04:24 PM Re: SQL QUERY for checking reservation details
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
wow thank you soo much!!! it does make sense and it worked perfect!!!!

that was too much logic for me lol i couldn't figure out but thanks!!
stivens is offline
Reply With Quote
View Public Profile
 
Old 05-20-2011, 08:13 AM Re: SQL QUERY for checking reservation details
Banned

Posts: 122
Name: devashish kumar
Trades: 0
Thank you for sharing your knowledge,this is such a useful tool.
devashishseo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL QUERY for checking reservation details
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.88803 seconds with 12 queries