0

我有以下代码使用从 A 到 B 的邮政编码的前 5 个字符检查“固定票价”。

所以如果LE675FE是输入,$full_post_code_a = LE675F;

基本上我想做的是一个循环,它将使用前 4 个和 3 个字符检查数据库,所以$full_post_code_a = LE675F / LE675 and LE67

只是想知道怎么可能做到这一点?

private static function checkFixedFarePC($post_codes) {

    self::$arr_postcodes = explode(",", $post_codes);

    // Get full postcode of Pickup

    $full_post_code_a = (self::$arr_postcodes[0]);

    // Get full postcode of Destination

    $full_post_code_b = (self::$arr_postcodes[1]);

    // Get first 5 characters of pickup & destination postcode

    $post_code_a = substr($full_post_code_a,0 ,5);

    $post_code_b = substr($full_post_code_b,0 ,5);

    // Pass postcodes to check if a fixed fare is possible

    return self::getFixedFare($post_code_a, $post_code_b);

}

哪个被发送到以下功能:

private static function getFixedFare($post_code_a, $post_code_b) {

$pdo = new SQL();
$dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

try {

    $query =   "SELECT VehicleSystemId, Fare FROM tblfixedfares
                WHERE ShortPostCodeA = '$post_code_a'
                AND ShortPostCodeB = '$post_code_b'
                AND DayHalf = :day_half
                AND VehicleSystemId IN ('Car', '6B')";

    $stmt = $dbh->prepare($query);

    $stmt->bindParam(':day_half', self::$day_half, PDO::PARAM_STR);

    $stmt->execute();

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);

    $stmt->closeCursor();

    $dbh = null;

    return $result;

}

catch (PDOException $pe) {
    die("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString);
}

任何帮助将非常感激!

4

2 回答 2

1

我只是想,我可以这样做:

$post_code_c = substr($full_post_code_a,0 ,4);
$post_code_d = substr($full_post_code_a,0 ,3);

然后将这些传递给查询:return self::getFixedFare($post_code_a, $post_code_b, $post_code_c);

然后将查询更改为:

WHERE ShortPostCodeA = '$post_code_a' OR ShortPostCodeA = '$post_code_c' OR ShortPostCodeA = '$post_code_d

虽然可能有更好的方法来做到这一点......

于 2012-11-30T13:56:15.163 回答
0
 WHERE 
 ShortPostCodeA IN 
 ('{$post_code_a}','{$post_code_b}')
于 2012-11-30T14:23:39.117 回答