Jump to content

Order by ID, not alphabetical


toocreepy

Recommended Posts

I'm pulling a list from a DB and inserting it in a drop down menu. Each item on the list has a numerical id associated called marketid in the DB. It works, but it orders the list alphabetically. I need it to order by marketid which is the column name. What code do I need to add? Here is what I have now....

 

<option value="<?=$market['marketid']?>"><?=$market['name']?></option>

 

Thanks in advance all!!

 

Tony

Link to comment
https://forums.phpfreaks.com/topic/225539-order-by-id-not-alphabetical/
Share on other sites

here's all the code for the drop down....

<select name="market[]" size="10" multiple="multiple" id="market[]">

    <?php

$markets = $crf->GetMarkets ();

foreach ($markets as $market)

{

?>

    <option value="<?=$market['marketid']?>"><?=$market['name']?></option>

<?php

}

?>

</select>

If you can modify the $crf->GetMarkets() method that would be preferable. Change it to add the SQL sort that taquitosensei suggested.

 

Otherwise, it gets a bit messy sorting a multidimensional array. You could try copying this function into your code:

http://www.exorithm.com/algorithm/view/sort_multi_array

Call it just before your foreach statement and pass $markets as the first parameter and 'name' as the second.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.