将数据库中选定的国家/地区放入选择框中
大家好,感谢您抽出时间。
我正在使用以下查询从我的数据库中获取信息:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能会出现此问题的解决方案:
代码端
您可以执行以下操作:
数据库
您还可以定制数据库查询以简单地选择国家/地区:
SELECT * FROM FROM amember_countries GROUP BY amember_countries.country
...或者类似的东西。
我个人建议数据库解决方案,因为这将是“正确”的解决方案(数据库是为了解决此类问题而创建的)。
Could potential solutions to this:
Code Side
You could do something like this:
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).
看起来您正尝试通过一个查询同时执行两件事:
amember_country
中的所有国家/地区。$username
的gold_profile.country
。如果您首先获取
$username
的信息,然后执行以下操作来获取国家/地区列表,您将会有更好的运气:GROUP BY 将折叠重复项。
然后,您需要更新您的
while
查找以使用$username
的国家/地区,而不是$row['userCountry']
来弄清楚所选的选项。Looks like you're trying to do two things at once with one query:
amember_country
.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: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.