Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created October 24, 2018 05:50
Show Gist options
  • Select an option

  • Save fyulistian/294a7db14a1066aeed93b349a7a450be to your computer and use it in GitHub Desktop.

Select an option

Save fyulistian/294a7db14a1066aeed93b349a7a450be to your computer and use it in GitHub Desktop.
Numeric range validator. Check the new value is available in the range or not with php.
<?php
// initialize
$range = array();
$range_price = array();
// data range
$range = array (
0 => array (
"min_range" => 10000,
"max_range" => 14000
),
1 => array (
"min_range" => 15000,
"max_range" => 17500
)
);
// get each range
foreach ($range as $key => $value) {
// initialize for minimum and maximum range
$min_range = $value["min_range"] - 1;
$max_range = $value["max_range"] + 1;
// check the minimum and maximum range
$new_start = min($max_range, max($min_range, $price_start));
$new_end = min($max_range, max($min_range, $price_end));
// check possibility
$parameters = ($new_start === $new_end) ? 1 : 0;
$range_price[] = $parameters;
}
// had possibility ?
if (in_array(0, $range_price, true)) {
$is_error = 1;
$error_msg = "Range available.";
} else {
// doSomething
// ...
// ...
// ...
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment