adam
adam

Reputation: 2968

How do I display the elments in a dropdown alphabetically rather than in order of ID in DB

I want to list the sites in the "ON" dropdown... alphabetically

http://virchoo.neighborrow.com/postcard

 <div class="line">on <select name="connection">
                <?php foreach($System->ClientHandler->getClientList() as $client): if($client->id != 4): ?>
                    <option value="<?= $client->id ?>"><?= $client->name ?></option>
                <?php endif; endforeach; ?>
                <option value="4">other</option>
            </select></div>

Upvotes: 1

Views: 3113

Answers (4)

Don Kirkby
Don Kirkby

Reputation: 56720

If you are selecting your client list from a database, add an ORDER BY to the query. If you're using some library and you can't sort it there, then you can probably assign the getClientList() result to an array and then sort that array before generating the select list in HTML. If your sorting isn't obvious, then you might need to use the usort() function.

Upvotes: 1

WTPK
WTPK

Reputation: 1079

You could use an "ORDER BY" clause to your SQL query.

SELECT * FROM clients ORDER BY name ASC

Upvotes: 2

Yohan P
Yohan P

Reputation: 494

Is getClientList() your code, and is this the only place you're using it? If so, sort the results in getClientList(). If not, copy the results of getClientList() to an array, sort it, and iterate through that instead.

Upvotes: 1

Nik
Nik

Reputation: 7273

Add an 'ORDER BY' clause to your query.

Upvotes: 3

Related Questions