2014-02-28 110 views
0

我有一個數組,我使用while循環提取並且我想比較兩個php變量。這兩個變量來自jQuery範圍滑塊。滑塊變量是:將數組值與變量比較

$startSlider; 
$endSlider; 

,並將它們與數組使用mysql_fetch_array使用While循環牽強:

$query2 = "SELECT startShift,endShift from seats where seatid=".$row['seatid']; 
$result2 = mysql_query($query2); 
    while ($row2 = mysql_fetch_array($result2)){ 
     echo $row2['startShift']."-".$row2['endShift']; 
     echo "<br>"; 
    } 

的,其結果將是:

Array of start shifts and end shifts for each block.

由於您可以在塊11中看到有兩組值/數組,因爲我有兩行具有相同的seatid但不同的startShift和endShift:

如何將它們與兩個範圍滑塊值進行比較。例如,比較720和360到$ startSlider和1080和600到$ endSlider。

我要的是:

IF $starSlider to $endSlider is not equal or overlaps $startShift to $endShift{ 
$blockColor = yellow;} 

我一直在試圖想出一個算法,但在PHP中我只是一個初學者。我希望我的問題很清楚。任何形式的幫助表示讚賞。先謝謝你。

示例: $ startSlider = 300; $ endSlider = 450;

範圍300-450在方框11中與範圍360-600重疊。如果360-600和720-1080範圍中的任何一個重疊。它會返回false。

+2

「重疊」是什麼意思?你能用數字解釋嗎?創建一個'overlap'的例子,看看它到底能帶來什麼。這並不難。 –

+0

在純技術層面上,比較是'$ row2 ['startShift'] <$ startSlider'。用你想要做的比較來替換'<'。除此之外,還不清楚你想以什麼方式進行比較。 – deceze

+0

我在最低的部分在我的帖子中添加了一個數字示例。我希望這是有道理的。 – rez

回答

0

我創造了一些代碼,試着和你的重疊測試幫助...

首先我創建了一個程序來檢查重疊範圍。這裏是代碼...

/* 
* 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 
0

不要覆蓋數組的索引創建新的索引,你可以比較值

0

我真的不明白你要實現(特別是,當你說的重疊,你是什麼意思)的東西。

但是,如果這將幫助..乾杯......

$query2 = "SELECT startShift,endShift from seats where seatid=".$row['seatid']; 
$result2 = mysql_query($query2); 
    while ($row2 = mysql_fetch_array($result2)){ 
     if($row2['startShift'] == $startSlider && $row2['endShift'] == $endSlider){ 

       Do your echo here and include html to define the colour. 

     } 

    }