Avatar of damianb123
damianb123
Flag 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
PHPMySQL ServerJavaScript

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Marco Gasi

Maybe people names are capitalized? try this:

sort($people, SORT_NATURAL | SORT_FLAG_CASE)

Open in new window

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
Ray Paseur

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
damianb123

ASKER
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
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.