Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/143062-solved-create-and-sort-array/
Share on other sites

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.