Forum Moderators: open

Message Too Old, No Replies

Filtering result to get only a unique first letter

Need to get rid of duplicate

         

henry0

4:05 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use the following to find the first letter of each username and then create a DD box with the selection
The query does fine
BUT if I have two usernames beginning for example with the letter "a" then the DD box will list both
How can I make sure that only one out of any number of similar first letters will be selected?

I tried to read the result as an array and use array_unique() which does not work for being within the loop each letter results in an array of its own and not in only one single array

$query = "SELECT UPPER(SUBSTRING(`username`,1,1)) AS letters FROM `profile` GROUP BY letters ORDER BY letters ASC";
echo $query;
$result=mysql_query($query);
echo '<select name="letters">';
while ($row=mysql_fetch_array($result) )
{
echo '<options value="'.$row['letters'].'">'.$row['letters'].'</option>';
}
echo '</select>';

Thanks

henry0

9:33 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A pure PHP solution has been found
however if you have a way to make it working by using a SQL solution
I will be very interested in learning about that solution.

syber

2:15 am on Oct 6, 2007 (gmt 0)

10+ Year Member



SELECT DISTINCT UPPER(SUBSTRING(`username`,1,1)) AS letters
FROM `profile`
ORDER BY letters ASC"

henry0

11:12 am on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



He he, that would have been too easy :)

Adding distinct does not work, does not result in error, and does not return any result.

Thanks for the try

blend27

3:29 pm on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



SELECT DISTINCT UPPER(SUBSTRING(username,1,1)) AS letters
FROM profile
ORDER BY letters ASC

WORKS LIKE A CHARM

----------------------
Added

SUBSTRING(`username`,1,1)) will return letter u, quotes need to be removed.

henry0

4:05 pm on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I simply echo the query and paste it in phpMyAdmin
it goes no where
result= row 0-0
I do not comment on your syntax (which looks like it should work)
but it does not deliver.

something else is echoed in phpMyAdmin
it reads:
the usual ->T<-
below it is the word: letters
and below letters:
a single cap letter: U which I believe comes from the first letter of username?
note: in the test col (username) nothing starts by or even contains a "u"

blend27

6:02 pm on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am not a PHP Programmer, but try this:

$query = "SELECT DISTINCT UPPER(SUBSTRING(username,1,1)) AS letters FROM profile ORDER BY letters ASC";

and then the rest of the code you have

the reason you get 0 records is cause you use
SUBSTRING(`username`,1,1)), which becomes a variable letters.

when you place the item in single quotes in SQL query it is treated as string and not a column name, so it creates a string 'U', wich is the first character in string 'username'(and then you sort by 'U').

that is not what you want.

you want distinct first letter in column username from table profile sorted ASC

give it a try.

blend27

henry0

6:43 pm on Oct 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This makes sense and works
thanks for the help