Reputation: 3763
See code below. I want to sort the option tags by name, not by id.
Here is my code:
<?php
$string = '<option id=a1>Sinead Shannon Roche<option id=a2>Emile Abossolo Mbo<option id=a3>Youssouf Djaoro<option id=a4>Dioucounda Koma';
$new_string = '';
$s = explode('<', $string);
asort($s);
foreach($s AS $v) {
$new_string = $new_string . '<' . $v;
}
// I want $new_string to be "<option id=a4>Dioucounda Koma<option id=a2>Emile Abossolo Mbo<option id=a1>Sinead Shannon Roche<option id=a3>Youssouf Djaoro"
?>
Upvotes: 0
Views: 316
Reputation: 366
I would use JQuery for this myself...
If you are intrested heres the code.
Declare a global function
$.fn.sortSelect = function() {
var op = this.children("option");
op.sort(function(a, b) {
return a.text > b.text ? 1 : -1;
})
return this.empty().append(op);
}
And call the function from the code.
$("#my_select").sortSelect();
Upvotes: 1