0

I am creating a simple drop-down menu displaying names:

    $sql = "SELECT id, 
                name
            FROM team 
            WHERE 1";
$stmt = $mysqli->prepare($sql);

if ($stmt) {
    $stmt->execute();    
    $stmt->store_result();

    $stmt->bind_result($id, $name);
    while ($stmt->fetch()) {
            echo '<option value=\"$id\">'.utf8_encode($name).'</option>';
    }       
} else {
    header('Location: ../../error.php?err=the database is corrupted');
}

I want to sort the option by alphabetic order. I am not sure which function should I use to sort the object before the loop. Any ideas?

4 Answers 4

2
$sql = "SELECT id, 
                name
            FROM team 
            WHERE 1 ORDER BY name ASC";
Sign up to request clarification or add additional context in comments.

Comments

2
$sql = "SELECT `id`, `name` FROM team 
WHERE 1 ORDER BY name";

Default order of Database is ASC, so it works even if you do not write ASC.

Comments

1

Do sorting in sql:

$sql = "SELECT id, 
                name
            FROM team 
            WHERE 1 order by name"

Comments

1

Use ORDER BY in your query to get names sorted in alphabetical order.

$sql = "SELECT id, name
        FROM team 
        WHERE 1
        ORDER BY name ASC";

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.