list ordering
Moderator: General Moderators
list ordering
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....
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
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
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.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Pardon. backwards loop example:
Never heard of anyone else call it anything, so I thought I might as well come up with a name
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
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK