I have a field that stores multiple zip codes. A query result for the zip codes column may contain several zip codes: 90027,90028,90068
I need an if statement to check if a single zip code is in the result
$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
}
};
If I read your question correctly, you want to distinguish between
#####
and
#####,#####,#####...
To do this, just use a regex to check if the field matches 5 digits.
if (preg_match("/^\d{5}$/", $zipresult[0])) {
...
}
Otherwise, as the others are saying, use in_array()
. What they're not saying is that you'd have to explode()
the string first, to make an array:
$exploded = explode(",", $zipresult[0]);
if (in_array($exploded, "99999")) {
....
}
EDIT per your comment you could use strpos()
$targetcode = "99999";
$found = array();
foreach ($zipresult as $row) {
if (strpos($row['pool_zip_codes'], $targetcode) !== false) {
$found[] = $row;
}
}
or in_array()
$targetcode = "99999";
$found = array();
foreach ($zipresult as $row) {
$exploded = explode(",", $row['pool_zip_codes']);
if (in_array($exploded, $targetcode)) {
$found[] = $row;
}
}
try this
$zipresult = mysql_fetch_array($zipqry);
if (($zipresult['pool_zip_codes']) == '90068') {
this zip code is in the list
} else {
not in list
}
use in_array()
as
if (in_array($zipresult[0],$zipresult))
{
echo "this zip code is in the list" ;
}
else {
echo "not in list";
}
Split the string and use in_array
:
if (in_array(explode(',', $zipresult[0]),$zipresult))
{
#this zip code is in the list
}
else {
#not in list
}