在两个数字之间搜索(百分比)

时间:2014-09-19 09:30:29

标签: php sql pdo

我有这个代码;

<?php
$username = "root";
$password = "Toom13371!";
$lowfreq = $_POST['Lowfreq']; # User-Supplied Data.             I'm A
$highfreq = $_POST['Highfreq']; # User-Supplied Data.        Placeholder yo.
$construction = $_POST['construction_type']; # User-Supplied Data.   Still holding
$connector = $_POST['connector_type']; # User-Supplied Data.                that place.
try {
    $dbh = new PDO("mysql:host=127.0.0.1;dbname=filters", $username, $password);
} catch(PDOException $e) {
    echo $e->getMessage();
}


//print
//echo $highfreq;
//echo $lowfreq;
//echo $connector;
//echo $construction; #tests

$condition = array(); #sets condition as array
$strcond = ""; #sets $strcond as "empty"
if (!empty($lowfreq)) {
    $condition[] = " start_passband = $lowfreq";
}

if (!empty($highfreq)) {
    $condition[] = " stop_passband = $highfreq";
}

if (!empty($construction)) {
    $condition[] = " construction_type = '$construction'";
}

if (!empty($connector)) {
    $condition[] = " connector_type = '$connector'";
}
if (!empty($condition)) {
$tempx = implode(' AND', $condition);
$tempy = " WHERE " . $tempx;
print $tempx;
unset($condition); #clears the array $condition
$condition= array(); 
$strcond = $tempy;
//print $strcond;
    //$condition .= " ".implode(' AND', $condition);
    }

    // Select some data
$sth = $dbh->prepare("SELECT id, type_code, connector_type, construction_type, start_passband,     stop_passband, lower_stopband_start, lower_stopband_stop, upper_stopband_start, upper_stopband_stop      FROM filter_bandpass $strcond");

// Execute the query, replacing the placeholders with their true value
$sth->execute(array());

我想要做的是使用$ lowfreq / $ highreq,然后在+/- 20%之间搜索,以返回其$ lowfreq +/- 20%然后$ highfreq +/- 20%的所有值。< / p>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果你问如何获得百分比:

$lowfreqlow = $lowfreq * 0.8;
$lowfreqhigh = $lowfreq * 1.2;

$highfreqlow = $highfreq * 0.8;
$highfreqhigh = $highfreq * 1.2;

然后像"... WHERE lowfreqcolumn BETWEEN ".$lowfreqlow." AND ".$lowfreqhigh." AND highfreqcolumn BETWEEN ".$lowfreqlow." AND ".$lowfreqhigh这样的新SQL查询。