我有一个表格,其中一列包含像文字一样的表面:" 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;"
但如果我的表面像&#34; 123 300 500 790 ......&#34;我该怎么办?只有一个选择是否可能?
由于
答案 0 :(得分:0)
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
}
答案 1 :(得分:0)
为什么不在查询中使用min / max?像这样:(未经测试)
$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