Solarpitch Posted March 18, 2008 Share Posted March 18, 2008 Hey guys, I have a little bit more of a complex problem below. I basically have this script that will take two time values, a start time and an end time and will then loop 10min intervals between both times and output the result... <?php $start = "7:00"; $finish = "12:00"; $st = strtotime(date('Y-m-d') . $start); $en = strtotime(date('Y-m-d') . $finish); $int = 10 * 60; $myinfo[0] = "<select name=\"time\" id='listboxes'>"; for ($i = $st; $i <= $en; $i += $int) { $time = date('h:i a',$i) . "<br>\n"; $myinfo[0] .= "<option value=\"".$time."\">".$time."</option>"; } ?> this will display a list box with 7:00, 7:10, 7:20, 7:30 and so on. What I want to do it to disinclude certain times from the loop. So if the value to disinclude is "7:10" I dont want that time to be in the list box. <?php $disinclude = "7:10"; --> needs to be a value like this. ........ ?> Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/ Share on other sites More sharing options...
MadTechie Posted March 18, 2008 Share Posted March 18, 2008 Basically just have an if statement to control it.. *Note code below was written online and is untested here is an example for a range of dates <?php $start = "7:00"; $finish = "12:00"; $st = strtotime(date('Y-m-d') . $start); $en = strtotime(date('Y-m-d') . $finish); //create a exclude range $excludeStart = "7:10" $excludeEnd = "7:40" $eStart = strtotime(date('Y-m-d') . $excludeStart; $eEnd = strtotime(date('Y-m-d') . $excludeEnd; $int = 10 * 60; $myinfo[0] = "<select name=\"time\" id='listboxes'>"; for ($i = $st; $i <= $en; $i += $int) { $time = date('h:i a',$i) . "<br>\n"; //if NOT in the exclude range then add a value if( !($time > $eStart && $time < $eEnd) ) { $myinfo[0] .= "<option value=\"".$time."\">".$time."</option>"; } } ?> another example for singles <?php $start = "7:00"; $finish = "12:00"; $st = strtotime(date('Y-m-d') . $start); $en = strtotime(date('Y-m-d') . $finish); //create a exclude single/s $exclude[] = "7:10" $exclude[] = "7:40" $int = 10 * 60; $myinfo[0] = "<select name=\"time\" id='listboxes'>"; for ($i = $st; $i <= $en; $i += $int) { $time = date('h:i a',$i) . "<br>\n"; //if NOT in the exclude array then add a value if( !in_array($time, $exclude) ) { $myinfo[0] .= "<option value=\"".$time."\">".$time."</option>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-494819 Share on other sites More sharing options...
Solarpitch Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks man, I'll have a look and try implement it shortly. Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-494822 Share on other sites More sharing options...
MadTechie Posted March 18, 2008 Share Posted March 18, 2008 lol you may want to add a semicolon to some of my lines ie $excludeStart = "7:10" $excludeEnd = "7:40" should be $excludeStart = "7:10"; $excludeEnd = "7:40"; Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-494834 Share on other sites More sharing options...
Solarpitch Posted March 20, 2008 Author Share Posted March 20, 2008 Hi MadTechie, I've tried both of the ways you suggested for this but the listbox just appears with the all the times in it and none excluded. Here's the full function... <?php function check_teetimes($teedate, $players, $membertype) { dbconnect(); $myinfo = array(); $sql = "select * from golfpro_time_sheet where realdate= '".$teedate."' and slots < ".$players.""; $result = mysql_query($sql) or die(mysql_error()); while(($row = mysql_fetch_row($result)) != false) { $myinfo[1] = $row[1]; } $start = "7:00"; $finish = "12:00"; $st = strtotime(date('Y-m-d') . $start); $en = strtotime(date('Y-m-d') . $finish); $exclude[] = "7:10"; $exclude[] = "7:40"; $int = 10 * 60; $myinfo[0] = "<select name=\"time\" id='listboxes'>"; for ($i = $st; $i <= $en; $i += $int) { $time = date('h:i a',$i) . "<br>\n"; //if NOT in the exclude array then add a value if( !in_array($time, $exclude) ) { $myinfo[0] .= "<option value=\"".$time."\">".$time."</option>"; } } $myinfo[0] .= "</select>"; return $myinfo; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-496695 Share on other sites More sharing options...
MadTechie Posted March 21, 2008 Share Posted March 21, 2008 change $time = date('h:i a',$i) . "<br>\n"; to $time = date('h:i a',$i); as that will messup the compare you can add it back in after the compare if( !in_array($time, $exclude) ) { $time = date('h:i a',$i) . "<br>\n"; $myinfo[0] .= "<option value=\"".$time."\">".$time."</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-497646 Share on other sites More sharing options...
Solarpitch Posted March 25, 2008 Author Share Posted March 25, 2008 Tried that there and it works fine. Thanks for your help with this! Quote Link to comment https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/#findComment-500189 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.