gin Posted November 27, 2006 Share Posted November 27, 2006 I'm building a scheduler, something along the lines of Google Calendar, where a user can input events and generally plan their day. One of the features I wish to add is a check for time clashing, where if two events overlap a small image alert will display. What I currently have is like so:1) query db and loop thru results2) for each and every event, query db again and look for clashes3) if there are clashes, add image to $output4) echo $outputThe actual problem is the query within a loop, which is an awful example of coding, I know. Is there some kind of... uh, funky recursive loop or something... that I can use? Any advise appreciated.This is the actual code, if anyone's interested:[code]<?php// sort of pseudo code// Get all events happening in a day.// For each event check if another event clashes with it, then output.$query = "SELECT * FROM events WHERE date='$today'";$result = mysql_query($query) or die ("Query failed: " . mysql_error());if ($myrow = mysql_fetch_array($result)) { do { $this_id = $myrow['id']; $ts = $myrow['timestart']; $te = $myrow['timeend']; $query2 = "SELECT id,timestart,timeend FROM events WHERE date='$today' AND id<>$this_id"; $result2 = mysql_query($query2) or die ("Query failed: " . mysql_error()); while ($myrow2 = mysql_fetch_array($result2)) { $ts2 = $myrow['timestart']; $te2 = $myrow['timeend']; // if event2 starts and ends totally before or after event1, then no clash if ( ($te2<=$ts) || ($ts2>=$te) ) { // no clash } else { // clash! $clash[$j] = $myrow['id']; $j++; } } if ($j>0) $clash = '<img src="clash.gif">'; echo $clash.' event details here'; // output event details } while ($myrow = mysql_fetch_array($result));}?>[/code]Edit: Edited for clarity. Quote Link to comment Share on other sites More sharing options...
gin Posted November 28, 2006 Author Share Posted November 28, 2006 bump Quote Link to comment Share on other sites More sharing options...
btherl Posted November 28, 2006 Share Posted November 28, 2006 Depending on the granularity of your times, you can use an associative array to record which events are at which times, like[code=php:0]$arr['20:05'] = 'event1';$arr['20:10'] = 'event1';[/code]And so on.. then, if any event needs to go in an array slot that's used, you know there's a clash. This approach requires only one pass through the events. But if you're allowing fine-grained times, it may be cumbersome to make all the array entries. Quote Link to comment Share on other sites More sharing options...
gin Posted November 28, 2006 Author Share Posted November 28, 2006 I'm not entirely sure what you mean by granularity but I think I get it. Users can only select times at half hour intervals, incidentally.So one pass to create an $output array and a $clash array, then loop thru the $output array while checking with the $clash array whether to output the additional image, right?Wow, still something of a headache, but doable now. Thank, I'll try this! Quote Link to comment Share on other sites More sharing options...
btherl Posted November 28, 2006 Share Posted November 28, 2006 Yep, exactly :) I think you get what I mean by granularity. You're using half-hour granularity, and I used 5 minutes in my example.I use this kind of technique a lot.. instead of looping over an array multiple times, I make an associative array where the array index is exactly what I'm looking for.If I want to check if a string is in an array (and will be doing it several times), then I do this:[code=php:0]$arr = array( 'string1' => true, 'string2' => true, .... 'string100' => true,);if ($arr[$test_string] === true) { print "Matched $test_string\n";}[/code]That's much faster than checking against all of 100 array entries for every string you want to check. It only works for exact matches though. Quote Link to comment 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.