Page 1 of 1
list ordering
Posted: Fri Dec 28, 2007 6:05 am
by yshaf13
hi,. I'm have a list of topics and each topic has an ordering number. i would like to add a up arrow and down arrow to each topic which on clicking would increment or decrement that topic's ordering number. how would i program this? the issue is, not necessarily are the order numbers sequential - the may be numbers skipped....
Posted: Fri Dec 28, 2007 6:33 am
by kaszu
One way could be to assign an order number to each item, and when you need to change order, swap numbers for those 2 items.
Code: Select all
$menu_items = array(
0 => array('name' => 'Item1', 'order' => 1)
1 => array('name' => 'Item2', 'order' => 2)
2 => array('name' => 'Item3', 'order' => 3)
);
function swap_order($item1_index, $item2_index, &$menu)
{
//..here add check if items exist...
$tmp_index = $menu[$item1_index]['order'];
$menu[$item2_index]['order'] = $menu[$item1_index]['order'];
$menu[$item1_index]['order'] = $tmp_index;
}
re
Posted: Fri Dec 28, 2007 6:51 am
by yshaf13
its not so simple - i load the list from the database, each topic already has a order number, to swap lets say topic 15 for the one before, i have to find out what the one before is - and not necessarily is it 14, so how do i access the row before and after the target row if its not +/- one?
just add a new column
Posted: Fri Dec 28, 2007 2:28 pm
by yacahuma
I dont know why you have an order that is not really an order. But is you want to display things in a certain order just add a new column to your table and use that for ordering.
Posted: Sat Dec 29, 2007 11:12 am
by Jonah Bron
Make the MySQWL query into a var, and loop backwards through it with
for.

re
Posted: Sat Dec 29, 2007 11:15 am
by yshaf13
sorry for my ignorance, but loop backwards??
Posted: Sat Dec 29, 2007 12:33 pm
by Jonah Bron
Pardon. backwards loop example:
Code: Select all
//normal loops
foreach($something as $something_else){
//do something with $something_else
}
for ($i=0;$i<count($something);$i++){
//do something with $something[$i]
}
//backwards loop:
for ($i=count($something);$i>=0;$i--){
//do something with $something[$i]
}
Never heard of anyone else call it anything, so I thought I might as well come up with a name

Posted: Sat Dec 29, 2007 9:48 pm
by Ollie Saunders
You need to swap the order numbers of the row you wish to move and it's neighbouring item above (if you're moving up) or below (if you're moving down).