从复选框中获取最低值

时间:2018-05-06 14:31:20

标签: php forms

该复选框提供skiresort的高度。在发布'之后(通过ajax)我希望获得最低价值的表格。这样做的方法是什么?

在接收端,我需要捕获值(作为整数):

       <?php
            //post

$altitudes = isset($_REQUEST['altitude']) ? $_REQUEST['altitude'] : array();
sort($altitudes, SORT_NUMERIC);


 foreach($altitudes as $value) {
  if $value > 0; $hohe = $value; break;
}

        ?>

<div class="list-group">
    <h3>Altitude</h3>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="500" type="checkbox"> &nbsp;&nbsp; 500
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="1200" type="checkbox"> &nbsp;&nbsp; 1200
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="1400" type="checkbox"> &nbsp;&nbsp; 1400
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="1800" type="checkbox"> &nbsp;&nbsp; 1800
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="2200" type="checkbox"> &nbsp;&nbsp; 2200
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="2600" type="checkbox"> &nbsp;&nbsp; 2600
    </a>
    <a href="javascript:void(0);" class="list-group-item">
        <input class="item_filter altitude"  name="altitude[]" value="3000" type="checkbox"> &nbsp;&nbsp; 3000
    </a>
</div>

1 个答案:

答案 0 :(得分:1)

遍历您的高度数组,抓取第一个值并将其指定为$min_val,然后检查每个后续值。如果下一个值小于$min_val,请将$min_val重新指定为该值。

$altitudes = isset($_POST['altitude']) ? $_POST['altitude'] : array();

if(!empty($altitudes)){
    foreach($altitudes as $value) {
        if(!isset($min_val) || $value < $min_val){
            $min_val = $value;
        } 
    }       
}

$min_val将是您的最低值