0

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"

?>

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.