1

複数の郵便番号を格納するフィールドがあります。郵便番号列のクエリ結果には、複数の郵便番号が含まれる場合があります: 90027、90028、90068

結果に単一の郵便番号が含まれているかどうかを確認するには、if ステートメントが必要です。

$zipstring = $rows['pool_zip_codes']);

$zipsql = "SELECT `pool_zip_codes` FROM `cases` WHERE `id` = '{$rowid}' AND `pool_zip_codes` IN ('{$zipstring}') ";

$zipqry = mysql_query($zipsql);
$zipresult = mysql_fetch_row($zipqry);

if (($zipresult[0]) == '90068') { 
this zip code is in the list
} else {
not in list
}
};
4

4 に答える 4

0

私があなたの質問を正しく読んだら、あなたは

#####

#####,#####,#####...

これを行うには、正規表現を使用して、フィールドが 5 桁に一致するかどうかを確認します。

if (preg_match("/^\d{5}$/", $zipresult[0])) {
    ...
}

それ以外の場合は、他の人が言っているように、を使用してin_array()ください。彼らが言っていないのはexplode()、配列を作成するには、最初に文字列を作成する必要があるということです。

$exploded = explode(",", $zipresult[0]);

if (in_array($exploded, "99999")) {
    ....
}

コメントごとに編集して使用できますstrpos()

$targetcode = "99999";
$found = array();
foreach ($zipresult as $row) {
    if (strpos($row['pool_zip_codes'], $targetcode) !== false) {
        $found[] = $row;
    }   
}

またin_array()

$targetcode = "99999";
$found = array();
foreach ($zipresult as $row) {
    $exploded = explode(",", $row['pool_zip_codes']);
    if (in_array($exploded, $targetcode)) {
       $found[] = $row;
    }
}
于 2013-10-10T07:20:16.333 に答える
0

これを試して

$zipresult = mysql_fetch_array($zipqry);

if (($zipresult['pool_zip_codes']) == '90068') {
this zip code is in the list
} else {
not in list
}
于 2013-10-10T07:22:03.687 に答える
0

使用するin_array()

なので

if (in_array($zipresult[0],$zipresult)) 
{ 
echo "this zip code is in the list" ;
} 
else { 
echo "not in list"; 
} 
于 2013-10-10T07:22:22.853 に答える