list ordering

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

list ordering

Post 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....
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post 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;
}
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post 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?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

just add a new column

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Make the MySQWL query into a var, and loop backwards through it with for.
:teach:
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

sorry for my ignorance, but loop backwards??
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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]
}
:wink:

Never heard of anyone else call it anything, so I thought I might as well come up with a name :D
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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).
Post Reply