theITvideos Posted October 8, 2010 Share Posted October 8, 2010 Hi there, I am working on a PHP web form and I have a combo box and I'm trying to sort the values in it. The values in the combo box are filled using an arraylist: $WeightArray = array('' => '', '0|100' => '100g or Less', '101|250' => '101g to 250g', '251|500' => '251g to 500g', '501|1000' => '501g to 1kg', '1000|2000' => '1kg to 2kg', '2000|3000' => '2kg to 3kg', '3000|4000' => '3kg to 4kg', '4000|5000' => '4kg to 5kg', '5000|6000' => '5kg to 6kg', '6000|7000' => '6kg to 7kg', '7000|8000' => '7kg to 8kg', '8000|9000' => '8kg to 9kg', '9000|10000' => '9kg to 10kg', 'customValues' => 'Custom Values' ); The values get properly filled in the combo box. Thats fine. Its just that the values are not sorted the way I want them to. The Grams and Kilograms are all mixed. It should output in the order defined in the array. See the ScreenShot I've taken and see the difference in the values defined and the output. I tried few sorting methods like ksort($WeightArray); But still the same output. Whats the good trick to sort values in Combo as per the array. All comments and feedback are always welcomed. Thank you. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2010 Share Posted October 8, 2010 It should output in the order defined in the array. Then simply iterate over the array. No sorting is necessary. Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120322 Share on other sites More sharing options...
theITvideos Posted October 8, 2010 Author Share Posted October 8, 2010 It should output in the order defined in the array. Then simply iterate over the array. No sorting is necessary. Thanks for the reply. I am bit newbie could you please tell me how I can iterate the values. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120324 Share on other sites More sharing options...
Rifts Posted October 8, 2010 Share Posted October 8, 2010 he just means put them in the order you want in the array Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120326 Share on other sites More sharing options...
theITvideos Posted October 8, 2010 Author Share Posted October 8, 2010 he just means put them in the order you want in the array It is already in order in the defined array. We need to properly iterate the values thats what he said. Now how do we iterate the values. Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120329 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 He meant just iterate over them with a foreach() loop. echo '<select name="whatever">'; foreach( $WeightArray as $k => $v ) { echo "<option value=\"$k\">$v</option>\n"; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120332 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2010 Share Posted October 8, 2010 Now how do we iterate the values. You have got to be kidding... You are already iterating over the values. Just remove the sort function. Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120333 Share on other sites More sharing options...
theITvideos Posted October 8, 2010 Author Share Posted October 8, 2010 Now how do we iterate the values. You have got to be kidding... You are already iterating over the values. Just remove the sort function. I've removed the sort function. And it didn't sort. And the way the code is written by ex-programmers is completely different from regular code. There is no regular combo box defined. The way the Arraylist values is called into a combo box is: 'ShipmentWeight' => array('type' => 'select', 'label' => 'Shipment Weight:', 'options' => $WeightArray) There is a whole list of arrays, just a mesh of arrays in the code. There is no regular: <select name="ComboName"> . . . </select> I probably cannot use the regular looping here. I just need to sort the arraylist values of i.e $WeightArray. What do you suggest after looking at some bits of my code. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120345 Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 I'd suggest you post as much of relevant the code as possible to eliminate the guesswork. Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120350 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2010 Share Posted October 8, 2010 The way the Arraylist values is called into a combo box is: ... Again, you cannot be serious. You have got a specific data definition that you didn't bother to share and some specific code that is processing that and you somehow want help with how it is sorting without showing anyone here that information? Quote Link to comment https://forums.phpfreaks.com/topic/215447-sorting-values-in-a-combo-box/#findComment-1120351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.