Compare table values ​​with variables

advertisements

I have an array that I fetched using While Loop and I want to compare to two php variables. The two variables are from the jQuery range slider. The slider variables are:

$startSlider;
$endSlider;

And compare them to an array fetched using mysql_fetch_array using the While Loop:

$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>";
    }

The result of that would be:

As you can see in block 11 there are 2 sets of values/array because I have two rows that have the same seatid but different startShift and endShift:

How do I compare them to the two range slider values. For example, compare the 720 and 360 to $startSlider and the 1080 and 600 to $endSlider.

What I want is:

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

I've been trying to come up with an algorithm but Im just a beginner in PHP. I hope my question is clear. Any kind of help is appreciated. Thank you in advance.

Example: $startSlider=300; $endSlider=450;

The range 300-450 overlaps the range 360-600 in block 11. If either of the 360-600 and 720-1080 range is overlapped. It would return false.


I created some code to try and help with your overlap test...

first i created a procedure to check for overlapping ranges. here is the code...

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

Next, i created a test database with the following structure and data in it:

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

Then i executed one seatid to ensure it did something sensible:

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

Here is the output from the above seatid of 8...

seatid: 8 seatRange: 360 - 600 result: source range OVERLAPS