Jump to content

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


antonyfal

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) {...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.