将数据库中选定的国家/地区放入选择框中

发布于 2024-11-03 14:23:45 字数 1076 浏览 1 评论 0原文

大家好,感谢您抽出时间。

我正在使用以下查询从我的数据库中获取信息:

$sql = "SELECT amember_countries.country, amember_countries.title, gold_profile.username, gold_profile.country AS userCountry 
        FROM amember_countries 
        LEFT JOIN gold_profile
        ON amember_countries.country = gold_profile.country

        ORDER BY amember_countries.title ASC
        ";

$rs = mysql_query($sql);

$selected = "";

while($row = mysql_fetch_array($rs))
{
    if ($row['userCountry'] == $row['country'] && $row['username'] == $username){
    $selected = "selected"; 
    }else{
    $selected = "";

    }
    echo "<option ".$selected." value=\"".$row['country']."\">".$row['title']."\n "; 
}

它工作正常,但我面临的问题是,如果 3 个人来自荷兰,则选择框将显示 3 次荷兰。对于其他国家来说也是如此。

例如:

user 1 is from The Netherlands
user 3 is from The Netherlands
user 6 is from The Netherlands

当单击选择框时,它会显示:

Belgium
Canada
Luxemburg
Netherlands
Netherlands
Netherlands

其他国家也发生同样的事情。

知道如何解决这个问题吗?

提前致谢!

Hi all and thanks for your time.

I'm using the following query to get information from my database:

$sql = "SELECT amember_countries.country, amember_countries.title, gold_profile.username, gold_profile.country AS userCountry 
        FROM amember_countries 
        LEFT JOIN gold_profile
        ON amember_countries.country = gold_profile.country

        ORDER BY amember_countries.title ASC
        ";

$rs = mysql_query($sql);

$selected = "";

while($row = mysql_fetch_array($rs))
{
    if ($row['userCountry'] == $row['country'] && $row['username'] == $username){
    $selected = "selected"; 
    }else{
    $selected = "";

    }
    echo "<option ".$selected." value=\"".$row['country']."\">".$row['title']."\n "; 
}

It works fine, but the problem I'm facing is if 3 people are from the Netherlands, the select box will show 3 times Netherlands. Same thing for the rest of the countries.

So for example:

user 1 is from The Netherlands
user 3 is from The Netherlands
user 6 is from The Netherlands

When clicking on the select box it shows:

Belgium
Canada
Luxemburg
Netherlands
Netherlands
Netherlands

The same thing is happening to the other countries.

Any idea how to solve this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2024-11-10 14:23:45

可能会出现此问题的解决方案:

代码端

您可以执行以下操作:

$a = array();
while($row = mysql_fetch_array($rs))
{
    if ($row['userCountry'] == $row['country'] && $row['username'] == $username){
    $selected = "selected"; 
    }else{
    $selected = "";

    }
    if(!in_array($row['country'], $a))
    {
       echo "<option ".$selected." value=\"".$row['country']."\">".$row['title']."\n "; 
       array_push($a, $row['country']);
    }
}

数据库

您还可以定制数据库查询以简单地选择国家/地区:

SELECT * FROM FROM amember_countries GROUP BY amember_countries.country

...或者类似的东西。

我个人建议数据库解决方案,因为这将是“正确”的解决方案(数据库是为了解决此类问题而创建的)。

Could potential solutions to this:

Code Side

You could do something like this:

$a = array();
while($row = mysql_fetch_array($rs))
{
    if ($row['userCountry'] == $row['country'] && $row['username'] == $username){
    $selected = "selected"; 
    }else{
    $selected = "";

    }
    if(!in_array($row['country'], $a))
    {
       echo "<option ".$selected." value=\"".$row['country']."\">".$row['title']."\n "; 
       array_push($a, $row['country']);
    }
}

Database

You could also tailor your database query to simply select the countries:

SELECT * FROM FROM amember_countries GROUP BY amember_countries.country

...Or something to that effect.

I would personally suggest the database solution as that would be the "correct" solution (databases are made to solve this kind of problem).

爱已欠费 2024-11-10 14:23:45

看起来您正尝试通过一个查询同时执行两件事:

  1. 获取 amember_country 中的所有国家/地区。
  2. 获取 $usernamegold_profile.country

如果您首先获取 $username 的信息,然后执行以下操作来获取国家/地区列表,您将会有更好的运气:

$sql = "SELECT country, title 
        FROM amember_countries
        GROUP BY country, title
        ORDER BY title ASC
        ";

GROUP BY 将折叠重复项。

然后,您需要更新您的 while 查找以使用 $username 的国家/地区,而不是 $row['userCountry'] 来弄清楚所选的选项。

Looks like you're trying to do two things at once with one query:

  1. Get all the countries from amember_country.
  2. Get gold_profile.country for $username.

You'll have better luck if you grab the information for $username first and then do this to get the country list:

$sql = "SELECT country, title 
        FROM amember_countries
        GROUP BY country, title
        ORDER BY title ASC
        ";

The GROUP BY will collapse duplicates.

Then you'll need to update your while look to use the country for $username rather than $row['userCountry'] to figure out the selected option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文