Same value in table, echo only one

Hey guys.

In my table i got strPlace, strCountry and some more, but thats not important.

And members can add places they been and then i want to loop the countries, but if someone added for example, 5 different places in one country, then I only want the country to show once, not 5 times.

What i got now is:

[php]$sql = mysql_query(“SELECT * FROM tblplace where strName = ‘$usr’”) or die(mysql_error());

while($list = mysql_fetch_array($sql))
echo $list[‘strCountry’];


Perhaps using array_unique?

But how can I use that for the loop?
when i try it, it just check one post, how can I match that post with all the other posts in the table, and only return the non-duplicates ?

Well, I don’t know what the data looks like coming back to you but I imagine you’ll need to assign the returned array to a new unique array and return that unique array. You would do that inside the while loop. There might be a better way to handle it and perhaps that’s what you tried but this was my first thought.

Thank you.
I will keep trying with array_unique and see if I can get it to work.

First, select just the fields you need, for example:

[php]$sql = mysql_query(“SELECT strName, Country FROM tblplace where strName = ‘$usr’”) or die(mysql_error());[/php]

Then you can use SELECT DISTINCT:

[php]$sql = mysql_query(“SELECT DISTINCT strName, Country FROM tblplace where strName = ‘$usr’”) or die(mysql_error());[/php]

That worked much better, and so easy. Thank you xerxes!

How can I use array_unique along with the provided script.

Sponsor our Newsletter | Privacy Policy | Terms of Service