Forum Moderators: coopster

Message Too Old, No Replies

Dynamic drop down box with result of str split()

Populating the box with the result

         

henry0

3:32 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried a zillion of solutions but the good one!
Here is what I need to achieve:
I use the result of a query, then get the first letter of each "row" and last need to populate a DD box with each of these first letter.

$query = "
SELECT username
FROM profile
ORDER by username ASC"; //echo"$query";
$result=mysql_query($query);

echo '<select name="username">';
echo '<option>';

while ($row=mysql_fetch_array($result) )
{
$username=$row['username'];
$username=$username{0};
$username=str_split($username);

echo"$username";
echo"</options>";
}
echo"</select>";

eelixduppy

3:51 pm on Oct 3, 2007 (gmt 0)



You should be echoing the open option tag in the loop like this:

echo "<option>$username</option>";

henry0

4:00 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Correct but the result in the DD box reads
array
array
array etc... as many times as a result is found

but not the first letter of each "username" as I look for

EDIT
IT WORKS
just removed the str_split part :)

[edited by: henry0 at 4:43 pm (utc) on Oct. 3, 2007]

whoisgregg

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

WebmasterWorld Senior Member 10+ Year Member



You could also do this using MySQL to do the work. (Plus the GROUP BY removes duplicates.)

$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>';

henry0

4:40 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks,
interesting concept (saving php actions)
I tried also substring (in the PHP) but did not go far.

FYI
The result as is now will be used to retrieve via a query all username starting by for ex: "a"
using %a

whoisgregg

5:54 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Figured that was how you were using it. ;)

henry0

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

WebmasterWorld Senior Member 10+ Year Member



I was looking to build a system that will only show
the first letter of existing account which I like better that searching a full list with empty query :)