cyrixware Posted February 23, 2008 Share Posted February 23, 2008 Hey guys... i have here the source codes. ('$startTime' BETWEEN startTime AND endTime) How to include this one into my query... This is my query: $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' "; Regards Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/ Share on other sites More sharing options...
Sulman Posted February 23, 2008 Share Posted February 23, 2008 Are you using timestamps for start and end times? If so you could do it like this: <?php $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND ('$startTime' > startTime AND '$startTime' < endTime)"; ?> However the fact that you have '$startTime' in quotes suggests that you are using strings... Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474329 Share on other sites More sharing options...
cyrixware Posted February 23, 2008 Author Share Posted February 23, 2008 Im not using Timestamps. i use TIME in my table structure. Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474332 Share on other sites More sharing options...
cyrixware Posted February 23, 2008 Author Share Posted February 23, 2008 Source Codes: <?php include("../connDB.php"); if($_REQUEST['Submit'] == "Generate Schedule") { $Subject = $_REQUEST['Subj']; $Section = $_REQUEST['Section']; $Room = $_REQUEST['Room']; $Day = $_REQUEST['Day']; $startTime = $_REQUEST['startTime']; $endTime = $_REQUEST['endTime']; if(($endTime < $startTime) || ($endTime == $startTime)) { ?> <script language="JavaScript"> alert("Please select another time."); //win = window.open('addSchedule.php','_self'); </script> <?php } else { /*$sql_search = "SELECT * FROM schedule WHERE Section = '$Section' && Day = '$Day' && Room = '$Room' || (startTime = '$startTime')"; $q_search = mysql_query($sql_search); $q_num = mysql_num_rows($q_search); if( $q_num != 0 ){ */ ?> <script language="JavaScript"> //win = window.open('addSchedule.php','_self'); //alert("Query result. File already exist!"); </script> <?php //} //else //{ $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' "; $q_add = mysql_query($sql); $num = mysql_num_rows($q_add); if( $num != 0 ){ ?> <script language="JavaScript"> //win = window.open('addSchedule.php','_self'); alert("Query result. Time conflict!"); </script> <?php } else { $sql = "INSERT INTO schedule (Section, Subject, Room, Day, startTime, endTime) VALUES ('$Section', '$Subject', '$Room', '$Day', '$startTime', '$endTime')"; if(!$q = mysql_query($sql)) { echo "<font color=#FF0000 size=2 face=Verdana, Arial, Helvetica, sans-serif> <center><b>Please select another time.</b></center></font>"; } elseif(mysql_affected_rows() == 0) { echo "<font color=#FF0000 size=2 face=Verdana, Arial, Helvetica, sans-serif> <center><b>Cannot add record this time...</b></center></font>"; } else { ?> <script language="JavaScript"> alert("Record has been succesfully saved! "); //win = window.open('addSchedule.php','_self'); </script> <?php } } //} } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474339 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 i think you could use strtotime. <?php $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND startTime > ".strtotime($startTime)." AND endTime < ".strtotime($endTime)." "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474345 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 or use the TIME_TO_SEC mysql function to compare the time difference... mysql.time <?php $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND startTime > TIME_TO_SEC($startTime) AND endTime < TIME_TO_SEC($startTime) "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474350 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 i had that mixed up i think <?php $sql = "SELECT * FROM schedule WHERE Section = '$Section' AND Day = '$Day' AND Room = '$Room' AND startTime < TIME_TO_SEC($startTime) AND endTime > TIME_TO_SEC($startTime) "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474351 Share on other sites More sharing options...
cyrixware Posted February 23, 2008 Author Share Posted February 23, 2008 guys stil the same problem. i use this one coz this is correct i think. '$startTime' BETWEEN startTime AND endTime But when i try to include this into the query i can save the time weither the time is conflict or not. $sql = "SELECT * FROM schedule WHERE Day = '$Day' AND Room = '$Room' AND ('$startTime' BETWEEN startTime AND endTime) "; by the way tnx fooDigi. is there any idea how to fix this one? Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474356 Share on other sites More sharing options...
cyrixware Posted February 23, 2008 Author Share Posted February 23, 2008 hi guys.. got it heres the solution: <?php$sql = "SELECT * FROM schedule WHERE Day = '$Day' AND Room = '$Room' AND ('$startTime' BETWEEN startTime AND endTime || startTime = '$startTime') ";?> Regards Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474513 Share on other sites More sharing options...
Barand Posted February 23, 2008 Share Posted February 23, 2008 $starttime BETWEEN starttime and endtime isn't the only conflicting condition [pre] DB DB starttime endtime ========================================================= conflicting bookings | | S----------E | | | | S---------E | | S--------------------E | | | S----E | | | ========================================================= OK bookings | | S------E | | | | S-----E [/pre] conflicts if (E > starttime) AND (S < endtime) Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474689 Share on other sites More sharing options...
cyrixware Posted February 24, 2008 Author Share Posted February 24, 2008 Thanks Barand. Got it. Quote Link to comment https://forums.phpfreaks.com/topic/92562-help-query-problem/#findComment-474866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.