strpos在表单上的错误

时间:2012-11-08 21:04:38

标签: php mysql

我正在为群组的位置编码,用户可以根据位置进行搜索,以找到最近的群组。领域是:国家,州,城市,邻里。假设在美国有十个团体 - 我不希望它列出选项美国十次。我添加了一个strpos,以便它只列出一次,但是我收到了一个错误。

这是php代码:

<?php
$myQuery = "select country, state, city, neighborhood from groups WHERE group_status = 'open to new members'";

$rs = mysql_query($myQuery);

$country_options = $state_options = $city_options = $neighborhood_options = '';

while($get_row = mysql_fetch_assoc($rs)){

$pos_country = strpos($get_row['country'], $country_options);

if($pos_country === false) {
echo $country_options .= "<option value='" . $get_row['country'] . "'>" . $get_row['country'] . "</option>";}

$pos_state = strpos($get_row['state'], $state_options);
if($pos_state === false) {
echo $state_options .= "<option value='" . $get_row['state'] . "'>" . $get_row['state'] . "</option>";}

$pos_city = strpos($get_row['city'], $city_options);
if($pos_city === false) {
echo $city_options .= "<option value='" . $get_row['city'] . "'>" . $get_row['city'] . "</option>";}

$pos_neighborhood = strpos($get_row['neighborhood'], $neighborhood_options);
if($pos_neighborhood === false) {
echo $neighborhood_options .= "<option value='" . $get_row['neighborhood'] . "'>" . $get_row['neighborhood'] . "</option>";}

}
?>

它输出以下错误:

  

警告:strpos():第66行的sidebar.php中的空分隔符

     

警告:strpos():第70行的sidebar.php中的空分隔符

     

警告:strpos():第73行的sidebar.php中的空分隔符

     

警告:strpos():第76行的sidebar.php中的空分隔符

在错误下方,它有一个很好的形式与正确的字段:国家,州,城市,邻里。它只是多次列出相同的国家。

1 个答案:

答案 0 :(得分:3)

delimiter strpos()是传入的第二个参数。

在您的代码中,您可以从:

开始
$country_options = $state_options = $city_options = $neighborhood_options = '';

这些是您用作分隔符的值,它们都是空的 - 因此您的错误。 之后,您使用给定的密钥执行strpos()检查,然后进行设置。例如:

$pos_country = strpos($get_row['country'], $country_options);
if($pos_country === false) {
    echo $country_options .= "<option value='" . $get_row['country'] . "'>" . $get_row['country'] . "</option>";
}

我不确定您使用$country_options搜索的是哪个字符,但是您会立即向其添加<option></option>标记(这是一个非常长的分隔符)。您是否可能使用错误的变量作为您要搜索的字符?

修改
重新阅读你的问题后,我理解你想要实现的目标(我认为)。您根本不想多次显示相同的国家/地区,州,城市或社区。

要实现这一点,可能更容易保留“看到”值的数组,并在每个循环中检查该数组。尝试这样的事情:

$countries = array();
$states = array();
$cities = array();
$neighborhoods = array();

while($get_row = mysql_fetch_assoc($rs)) {
    if (!in_array($get_row['country'], $countries)) {
        $country_options .= '<option value="' . $get_row['country'] . '">' . $get_row['country'] . '</option>';
        $countries[] = $get_row['country'];
    }

    if (!in_array($get_row['state'], $states)) {
        $state_options .= '<option value="' . $get_row['state'] . '">' . $get_row['state'] . '</option>';
        $states[] = $get_row['state'];
    }

    if (!in_array($get_row['city'], $cities)) {
        $city_options .= '<option value="' . $get_row['city'] . '">' . $get_row['city'] . '</option>';
        $cities[] = $get_row['city'];
    }

    if (!in_array($get_row['neighborhood'], $neighborhoods)) {
        $neighborhood_options .= '<option value="' . $get_row['neighborhood'] . '">' . $get_row['neighborhood'] . '</option>';
        $neighborhoods[] = $get_row['neighborhood'];
    }
}