Jump to content

how can i sort this alphabetically, and remove spaces? select list query


Recommended Posts

Hi i have a drop-down list this little piece is where i explode the comma deliminated words into an array..

 

everything works but i cant find a function to do the sort order of the value options? and also i get a blank space everytime.. as option and value?

<optgroup label=\"Others added\">";										
		$profile_languagegeneralrep = str_replace(', ', ',',$profile_languagegeneral); 
        $langwording = explode(',', $profile_languagegeneralrep);
        $langwording = array_unique($langwording);
        foreach($langwording as $langexi)				
	$languageLists .= "<option value=\"".$langexi.','."\">".$langexi."</option>";
	$languageLists .= "</optgroup>";
			}
		$languageLists.="</select>";		   	
		$output['LANGUAGELISTS']=$languageLists;

The "blank space" as option and value is most likely the result of having a comma at the beginning or end of your original string. The explode() function splits the string on ALL occurrences of the delimiter. To prevent this from happening, remove the leading and/or trailing comma. You can do this with trim(). Based on the code from  requinix:

 

"<optgroup label=\"Others added\">";
$langwording = array_unique(array_map("trim", explode(",", trim($profile_languagegeneral, ','))));
sort($langwording);
foreach ($langwording as $langexi) {...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.