0

If I have 4 db rows, I want to be able to sort them using column 'position'.

id - name - position

1  - row1 -    1
2  - row2 -    2
3  - row3 -    3
4  - row4 -    4

I know how to sort with php SELECT SORT BY etc

But I want to be able to press a hyperlink which would swap positions.

Row   img     img
row1  (down)
row2  (up)    (down)
row3  (up)    (down)
row4  (up)

ie, Press up on row 2 would change row2 position -> 1 and would change row 1 position -> 2

Any help would be greatly appreciated.

6
  • What exactly do your want? Is it just a JavaScript you're interested in? Or PHP code + database structure for re-arranging data in a database table? Commented Feb 9, 2012 at 11:42
  • either, was after advice into what ppl thought was the best solution. If you have ideas pls let me know. Commented Feb 9, 2012 at 12:38
  • Understood, but the way you've put your question it seems like people are thinking this is a JavaScript/GUI question. You should re-phrase your question, emphasizing that your have a position column and want a solution to manipulate its value. Commented Feb 9, 2012 at 13:29
  • Are you still looking for an answer in PHP? Commented Sep 9, 2013 at 13:48
  • @fruityp: I would like to send via ajax to update SQL Commented Nov 7, 2013 at 12:03

5 Answers 5

0

On the hyperlink, load a PHP page which takes a few parameters:

  1. The id of the row you want to move up/down
  2. The current position of the row you want to move up/down
  3. The id of the row beneath/above the row you want to move
  4. The current position of the row you want to move up/down
  5. Whether you want to move up or down of the row you're moving (such as its id), and the current position, as well as the new position.

From this, you will know exactly what you need to do, and can fashion a query to swap these items in the database. After this is done, reload the page.

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest thing to do is to use a javascript library that allows item reordering. Check this out: http://jqueryui.com/demos/sortable/

Once the user has finished reordering, you can save the current positions of all your elements. Using this method allows the user to rearrange all the items as he likes without having to refresh the page each time he clicks on "Up" or "Down".

3 Comments

Thanks, sounds good. How would this save for the next time they visit the page.
After the user has finished ordering the items, he should click on a button/link to save the current order. On that event, you need to serialize the items and send them to the server.
If you need more info about saving the ordered items, I just found another question exactly about that: stackoverflow.com/questions/7342727/…
0

um what about this

while ($row = mysql_fetch_array($query)) {
@$group[] = $row['img'];
}

and call it
echo $group[1];
echo $group[3];
echo $group[0];
echo $group[2];

etc. idk if this what you meant

Comments

0

if you want to swap the position in the UI only, you can use javascript.

eg:

<div id="1" > <div id="data_1">data goes here for row1</div> </div>
<div id="2" > <div id="data_2">data goes here for row2</div> <a onclick="moveUp(2);"> UP</a> <a onclick="moveDown(2);"> Down</a></div>
<div id="3" > <div id="data_3">data goes here for row3</div> </div>

<script>
function moveUp(n){
  var m = n-1;
  var own_id = 'data_'+n;
  var new_id = 'data_'+m;
  var tmp_data = '';
  var own_data = $(own_id).innerHtml;
  if($(new_id) != 'undefined'){
    tmp_data = $(new_id).innerHtml;
    $(new_id).innerHtml = own_data;
    $(own_id).innerHtml = tmp_data;
  }

}

function moveDown(n){
  //same logic can be applied here
}
</script>

hope it will help.

Comments

0

I also meet the same problem and you can try this kind of solution. http://www.phpfreaks.com/tutorial/php-custom-list-order

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.