将数组值与变量进行比较

时间:2014-02-28 10:07:27

标签: php arrays

我有一个使用While Loop获取的数组,我想比较两个php变量。这两个变量来自jQuery范围滑块。滑块变量是:

$startSlider;
$endSlider;

将它们与使用While循环使用mysql_fetch_array获取的数组进行比较:

$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。

3 个答案:

答案 0 :(得分:0)

不要覆盖数组的索引创建新索引,您可以比较值

答案 1 :(得分: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.

        }

    }

答案 2 :(得分: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
  */

然后我执行了一个海藻以确保它做了一些明智的事情:

/*
 * 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: 8 seatRange: 360 - 600 result: source range OVERLAPS