Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ordering a list of items. I am nearly there but there's something worth a second pair of eyes

0 views
Skip to first unread message

McKaulick

unread,
Nov 4, 2008, 7:42:33 AM11/4/08
to
Hello,

I have a list of 3 items. When the html page is loaded, there are 3
items with their order

position 1 (ID 5)
position 2 (ID 8)
position 3 (ID 6)

I change 3 to 1 and click submit.

Result should be

position 1 (ID 6)
position 2 (ID 5)
position 3 (ID 8)

The form looks like this.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// Here I execute the select query order by PORTFOLIO ORDER
foreach {
echo "<tr><td><input type='text' name='item_order[]' value='".
$row->PORTFOLIO_ORDER."'>";
echo "<input type='hidden' name='item_uid[]' value='".$row-
>PK_PORTFOLIO_UID."'></td><tr>";
}

?>
<submit button>
</form>

Here's the code that suppose to reorder the list.

if (isset ($_POST['item_order'])) {

//Here I print the original value
print_r( $_POST['item_order']); //Array ( [0] => 1 [1] => 2
[2] => 1 )

// Here I sort the value
asort($_POST['item_order']);

print_r( $_POST['item_order']); //Array ( [0] => 1 [2] => 1
[1] => 2 ) *So Far so good*

foreach ($_POST['item_order'] as $key => $value) {
$key += 1;
$new_order[] = $key;
}
foreach ($new_order as $key2 => $order2) {
$item_id = $_POST['item_uid'];
print $item_id[$key2]."----".$order2; // Will print
in order: 5----1 / 8----3 /6----2 (WRONG)
}

}

ID 6 should be 1 and not 2. What am I doing wrong here? Someone has a
clue for me? Would be really appreciated.

Thanks!

Jerry Stuckle

unread,
Nov 4, 2008, 8:50:35 AM11/4/08
to

You now have 2 items with an id of 1. How does the code know which
should come first?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

McKaulick

unread,
Nov 4, 2008, 11:27:03 AM11/4/08
to
Hello Jerry, thanks for you reply. I looked at the code again and I
must say, not only I am stuck, I am wondering is there a better way to
do this. Got any idea?

Thanks!

> jstuck...@attglobal.net
> ==================

Jerry Stuckle

unread,
Nov 4, 2008, 12:34:33 PM11/4/08
to

(Top posting fixed)

Well, let's think about it a little. When you change the position, you
need to move other items up or down.

One way to do this is to set up a counter with the current position and
go down the list, checking the current position against the desired one.
If there is a match, just continue on.

If there is a mismatch and the desired position is higher, move that one
to the desired position and move all of the items between the current
and desired position down one. If the desired position is lower, move
it down and move the items between up one position.

Another way would be to check the position, and if it's higher than the
current position, set the value to desired position - 0.5. If it's
lower set the value to the desired position + 0.5. Sort your array and
go back and renumber them 1-n.

But both of these can have problems if you move more than one item at a
time. The way I prefer is to just have up and down arrows next to the
position. Clicking on the up arrow will move the item up one position
(basically swapping the positions of the item and the one in front of
it), while clicking on the down arrow moves it down one position.

This suffers from the problem of a lot of clicks if you have a large
number of items and need to move one up a ways (or need to move several
items around). But it solves the problem with moving 2 at the same time.

Take your pick.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

jstu...@attglobal.net
==================

0 new messages