检查表数组值是否在范围内


check if table array value in range

我有一个表格,其中一列包含文本等表面:"200 300 450 557"用户搜索表面给出我得到的 2 个变量$min和$max的范围。我需要选择在该区间内具有表面的所有属性。

在这里,我有一个较长的解决方案,但我相信在选择中有一个较短的解决方案......

$min=$_GET["min"]; // min price given by user - like 1000 euro
$max=$_GET["max"]; // max price given by user - like 2500 euro
$result=mysql_query("SELECT * FROM `offers`");
$nr=mysql_numrows($result);
while( $i<$nr) {
    $surface=mysql_result($result,$i,"surface"); // something like "100 200 250 350" sqm
    $price=mysql_result($result,$i,"price"); // this is per square metter like "12" euro/sqm
    $s=explode(' ',$surface);
    if ($s[$i]*$price<=$max && $s[$i]*$price>=$min) // i don't know if it sees it like number or char???
    {
        $id = mysql_result($result,$i,"id");
        $area = mysql_result($result,$i,"area");
        $details = = mysql_result($result,$i,"det");
        // do something with them
    };
}

关键是我需要最简单的解决方案来进行多次搜索。如果我有一个单一的表面,代码将是这样的:

"SELECT * FROM `offers` WHERE (surface between $min and $max) && (price between $pmin and $pmax)&&(area='$area') ORDER BY `id` DESC LIMIT 50;"

但是,如果我有像"123 300 500 790..."这样的表面,我该怎么办?是否可以只选择一次?

谢谢

对数组使用 min 和 max

if((min($s) * $price) >= $min && (max($s) * $price) <= $max)
{
    $id = mysql_result($result,$i,"id");
    $area = mysql_result($result,$i,"area");
    $details = = mysql_result($result,$i,"det");
    // do something with them
}

为什么不在查询中使用最小值/最大值?像这样:(未经测试(

$min=$_GET["min"]; // min price given by user - like 1000 euro
$max=$_GET["max"]; // max price given by user - like 2500 euro
// select from offers where price > min and price < max
$result=mysql_query(sprintf("SELECT * FROM `offers` WHERE `price` > %d AND `price` < %d",
    mysql_real_escape_string($min),
    mysql_real_escape_string($max)
));
$offers=mysql_numrows($result);
// loop over the offers
foreach($offers as $offer){
    $id = $offer['id'];
    $price = $offer['price'];
    // do something 
}

现在请记住,mysql_query()已被弃用。我建议做一个PDO教程。

PDO教程:http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059