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>
Add an 'ORDER BY' clause to your query.
You could use an "ORDER BY" clause to your SQL query.
SELECT * FROM clients ORDER BY name ASC
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.
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.
精彩评论