Link to home
Start Free TrialLog in
Avatar of damianb123
damianb123Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Issue getting array to sort alphabetically

Hi,
    I have a select statement which pulls the content from my database.  one of the fields has several entities per field (names of people) separated by a comma, to which I use an explode routine and form a select drop down box for users to choose the name of the person they want.....

Here's the code for the select statement:

SELECT * FROM maindata

Open in new window


And here is the code making up the select box - which works great, it just doesn't sort....

<?php
echo "<select name='personID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px'>";
   echo "<option selected='selected' value='allpeople'>See all People</option>";
   while ($list = mysql_fetch_assoc($result)) {

	$people = array_filter( explode(",", $list['People']), 'strlen' );
    	    sort($people);  
      foreach($people as $person) {
		   
         echo "<option value='$person'>$person</option>";
      } 

   } // end while 
echo "</select>";
?>

Open in new window


Can anyone help?

Thanks
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Maybe people names are capitalized? try this:

sort($people, SORT_NATURAL | SORT_FLAG_CASE)

Open in new window

Avatar of damianb123

ASKER

Hi Marco,  the names of the people are in this format:

Mr H Hues, Mr D Winter, Mr J Tominson, Mr E Langeldt, Mr K Ran, Mr S Ahmed, Mr P Wong, Mr I Ching, Mr F Hirani

I would expect any sort to go by the initial i.e. H, D, J etc. so Mr D winter, Mr E Langeldt etc.

But even with your code above like this, it still doesn't seem to work:

   while ($list = mysql_fetch_assoc($result)) {

	$people = array_filter( explode(",", $list['People']), 'strlen' );
    	    sort($people, SORT_NATURAL | SORT_FLAG_CASE);  
      foreach($people as $person) {

Open in new window


Am I doing something wrong?  Thanks
There is a natcasesort() function in PHP, but it seems that you would want to sort by last name. then by initials (honorifics and titles are rarely sorted, but are more often ordered by rank - I can't see a good way to automate that).

Can we rely on some variant of the example here as a suitable test case?

Mr H Hues, Mr D Winter, Mr J Tominson, Mr E Langeldt, Mr K Ran, Mr S Ahmed, Mr P Wong, Mr I Ching, Mr F Hirani
Hi Ray,  to be honest sorting by the initial would suffice, just to try and get it in some sort of order.   It doesn't seem to matter what I try though, it fails to sort.

Can you help?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial