我創造了一些代碼,試着和你的重疊測試幫助...
首先我創建了一個程序來檢查重疊範圍。這裏是代碼...
/*
* A range is declared as an associated array as follows
* array(
* 'start' => <integer value>
* 'end' => integer value>
* )
*
* where 'start' is always less than or equal to 'end'.
*
* e.g. array('start' => 350, 'end' => 450)
*
*/
/*
* To show the logic more clearly we will create a function that detects whether
* ranges overlap or not as follows:
*
* It will accept two ranges:
* 1) a 'target' range that is being tested against.
* 2) a 'source' range that may overlap the 'target' range
*
* The function will return a single integer as follows:
*
* result
* -1 ==> the 'source' range is completely outside the 'target' range
* 0 ==> the 'source' range overlaps the 'target' range
* +1 ==> the 'source' range is completely inside the target range
*
* The function is not the most efficient but hopefully is clear to understand.
*/
function check_range_overlap(array $targetRange, array $sourceRange)
{
// is the source range completely outside?
$isOutside = $sourceRange['end'] < $targetRange['start']
|| $sourceRange['start'] > $targetRange['end'];
// is the source range completely inside?
$isInside = $sourceRange['start'] >= $targetRange['start']
&& $sourceRange['end'] <= $targetRange['end'];
// now see which of the above are true. if none are then the source overlaps overlaps.
// i am not interested in how much the range overlaps
if ($isOutside) {
$overlapResult = -1; // completely outside
}
elseif ($isInside)
$overlapResult = 1; // completely inside
else
$overlapResult = 0; // overlaps somehow - i do note care how much or where
return $overlapResult;
}
/*
* Test it...
*/
/* */
$target = array('start' => 100, 'end' => 200);
// completely outside less than
$source = array('start' => 10, 'end' => 90);
echo '<br/>Outside test: less than: ', check_range_overlap($target, $source) === -1 ? 'true' : 'false';
// completely outside greater than
$source = array('start' => 300, 'end' => 400);
echo '<br/>Outside test: greater than: ', check_range_overlap($target, $source) === -1 ? 'true' : 'false';
// completely inside - smaller range
$source = array('start' => 110, 'end' => 190);
echo '<br/>Inside test: smaller range: ', check_range_overlap($target, $source) === 1 ? 'true' : 'false';
// completely inside - equal range
$source = array('start' => 100, 'end' => 200);
echo '<br/>Inside test: equal range: ', check_range_overlap($target, $source) === 1 ? 'true' : 'false';
// overlap - start only
$source = array('start' => 50, 'end' => 120);
echo '<br/>Inside test: equal range: ', check_range_overlap($target, $source) === 0 ? 'true' : 'false';
// overlap - end only
$source = array('start' => 150, 'end' => 220);
echo '<br/>Inside test: equal range: ', check_range_overlap($target, $source) === 0 ? 'true' : 'false';
// overlap - start and end .i.e larger than target.
$source = array('start' => 90, 'end' => 220);
echo '<br/>Inside test: equal range: ', check_range_overlap($target, $source) === 0 ? 'true' : 'false';
/* */
/*
* --------------------------- Function definition and test end ------------------
*/
接下來,我創建了下面的結構和數據中有一個測試數據庫:
/*
* Table seat details:
*
* id seatid startShift endShift
------ ------ ---------- ----------
1 11 720 1080
2 11 360 600
3 9 720 1080
4 8 360 600
5 90 100 200
6 91 200 300
7 92 300 400
*/
然後我執行一個seatid,以確保它做了一件明智的:
/*
* now the mysql database stuff...
*/
$db = new mysqli('localhost', 'test', 'test', 'testmysql');
$seatIdQuery = $db->prepare('SELECT seatid, startShift,endShift from seats where seatid = ?');
if ($db->errno) {
echo '<br/>prepare error: ', $db->errno, ' : ', $db->error, '<br/>';
}
$currentSeatId = 0; // current seat id we will use
// bind the currentSeatId to the 'seatIdQuery'
$seatIdQuery->bind_param('i', $currentSeatId);
/*
* This is the start of the code to process one source range against one seatid and display the results
*/
$currentSeatId = 8; // current seat id
$allOk = $seatIdQuery->execute(); // execute the prepared query with the seatId of 8
$results = $seatIdQuery->get_result(); // get the query result set
$sourceRange = array('start' => 350, 'end' => 450); // source range -- from slider...
while ($row = $results->fetch_assoc()) { // for each seatid row returned
// check against the 'seatid' range
$targetRange = array('start' => $row['startShift'], 'end' => $row['endShift']);
$overlapCheck = check_range_overlap($targetRange, $sourceRange);
// prepare range overlap message
if ($overlapCheck < 0) {
$overlapText = 'source range is Outside.';
}
elseif ($overlapCheck >= 1) {
$overlapText = 'source range is Inside';
}
else {
$overlapText = 'source range OVERLAPS';
}
// show the result
echo '<br/>seatid: ', $row['seatid'],
' seatRange: ', $row['startShift'], ' - ', $row['endShift'],
' result: ', $overlapText;
}
/*
* This is the end of code to process one source range against one seatid and display the results
*/
這裏是8以上seatid輸出...
seatid: 8 seatRange: 360 - 600 result: source range OVERLAPS
「重疊」是什麼意思?你能用數字解釋嗎?創建一個'overlap'的例子,看看它到底能帶來什麼。這並不難。 –
在純技術層面上,比較是'$ row2 ['startShift'] <$ startSlider'。用你想要做的比較來替換'<'。除此之外,還不清楚你想以什麼方式進行比較。 – deceze
我在最低的部分在我的帖子中添加了一個數字示例。我希望這是有道理的。 – rez