frost Posted January 30, 2009 Share Posted January 30, 2009 Hi, I am rubbish at arrays so please bear with me. I have a set of selections that can be assigned numbers for showing: <input type=text name=item1> Item 1 <input type=text name=item1> Item 2 The user can assign a sort order to the items, upon submitting the form I would like to pop these into a database as a sorted array. So if they assigned item 1 a value of 5 and item 2 a value of 4 the array would go item2, item1 Any help would be really appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/ Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 I think you just want to use sort() Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750286 Share on other sites More sharing options...
frost Posted January 30, 2009 Author Share Posted January 30, 2009 But I need to sort them based on the value supplied by the user right? how would that work? Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750301 Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 The html should be; <input type="text" name="item[]"> Item 1 <input type="text" name="item[]"> Item 2 The on the page receiving this data use $the_number = count($_POST['item']);//that is assuming you;re using POST Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750306 Share on other sites More sharing options...
frost Posted January 30, 2009 Author Share Posted January 30, 2009 But that will just create an array of the values supplied by the user and ignores the item name. Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750335 Share on other sites More sharing options...
phparray Posted January 30, 2009 Share Posted January 30, 2009 Give this a shot <input type="text" name="item[1]"> Item 1 <input type="text" name="item[2]"> Item 2 <input type="text" name="item[3]"> Item 3 <input type="text" name="item[4]"> Item 4 <?php if(isset($_POST['item'])) { arsort($_POST['item'],SORT_NUMERIC); $sortedArray = array_reverse($_POST['item'],true); foreach($sortedArray as $item => $order) { echo 'Item number: '.$item.' Ordered value: '.$order.'<br />'; } } ?> This stores the item number in a var called $item and the order by value in the var called $order. Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750353 Share on other sites More sharing options...
frost Posted January 30, 2009 Author Share Posted January 30, 2009 Cheers! I think that gets me where I need to be. Quote Link to comment https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/#findComment-750689 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.