Welcome Guest, Not a member yet? Register   Sign In
Re-ordering result (rank)
#1

[eluser]Référencement Google[/eluser]
Hi all,

I would like to know before starting make it how would you handle a re-ordering of results (not ASC or DESC, but a rank position)

In my DB table I put a SMALLINT field named "rank", I think it is a start but I have difficulties imagining how I will handle that with PHP changing this rank number when the user click an arrows to reorder how he wants.

Any ideas are welcome!
#2

[eluser]Rick Jolly[/eluser]
This kind of thing is so easy to do with the help of javascript. You could let the user dynamically update the order, and when finished submit the changes to the database.

I've used scriptaculous to update the order of images using drag and drop. When the user submits the changes, scriptaculous posts an array of image ids in the current order. Then all I have to do is loop through the array and update the order:
Code:
// 'pic_ids' holds a string representing an ordered array of image ids like this: pics[]=2&pics;[]=3&pics;[]=1
$pic_ids = $this->input->post('pic_ids');
// parse_str($pic_ids) creates and assigns the pics array from the $pic_ids string.
parse_str($pic_ids);
if ($pics)
{
   $count = count($pics);
   for ($i = 0; $i < $count; $i++)
   {
      $this->db->where('pic_id', $pics[$i]);
      $this->db->update('pictures_table', array('picture_order' => $i));
   }
}

EDIT: Never mind the semicolons in the pics array example above - the forum is adding those.
#3

[eluser]Référencement Google[/eluser]
That a nice idea, but my app won't use javascript (client restrictions) So I must to do that with hard coded PHP. Any ideas ?
#4

[eluser]ELRafael[/eluser]
if you want to re-order according the user choice, you can pass the value on click in the href of page...

Code:
<a href="same_page/orderby/1">Order By 1

and in the controller, check if there's any value in orderby... if, modify the db search

is this your doubt?
#5

[eluser]Référencement Google[/eluser]
ELRafael you didn't understood the point. I will try to explain more:

It's not according to the user choice, it is for a backend, letting the admin decide in wich order will appear results for the users, so the order of each results have to be recorded in the DB permanently. For that I created a DB column "rank" and while displaying view to user simply ORDER BY rank

My problem isn't in the public site where users view results but it is on how to handle this for the admin pannel, for exemple with arrows up and down while clicking this move results up or down and then how to do it with PHP
#6

[eluser]ELRafael[/eluser]
[quote author="elitemedia" date="1190855880"]ELRafael you didn't understood the point. I will try to explain more:

It's not according to the user choice, it is for a backend, letting the admin decide in wich order will appear results for the users, so the order of each results have to be recorded in the DB permanently. For that I created a DB column "rank" and while displaying view to user simply ORDER BY rank

My problem isn't in the public site where users view results but it is on how to handle this for the admin pannel, for exemple with arrows up and down while clicking this move results up or down and then how to do it with PHP[/quote]

oooooooops 8-/

ok, you need something like this:

- There's a list of products, with these 3 options to rank: Name, Price, Size
- Another list of person, with Name, City and Age
- The price must appears first than size and name.

But you can't use javascript, right?

I don't see much difficult... Put on the arrows (up and down) a href to php that re-orders (update the field rank in your db).

If is not this your doubt, i give up :zip:
#7

[eluser]Rick Jolly[/eluser]
No javascript - that's a shame. It might be a little annoying to have a page refresh every time someone wants to reorder one item by clicking an arrow link, and without javascript that's what you'd be stuck with.

You'll have to reorder all ranks prior to updating one (to create a space for the updated rank) since otherwise the order could get corrupted because rank numbers could get duplicated. I had to do this once, I'll try to find the code when I have time.
#8

[eluser]Référencement Google[/eluser]
[quote author="ELRafael" date="1190856727"]Put on the arrows (up and down) a href to php that re-orders (update the field rank in your db).[/quote]

ElRafael, that's exactly on this point I need some help and advices, I knew before that I will have to update the field rank in the DB, but how?

Simply make a "rank - 1 or rank +1" won't work because I will have like that 2 ranks equal, it's not the solution. And what when the rank comes to 0 ?

Rick, I am waiting for a piece of code if you can find it again, it would be nice from you, thanks in advance !
#9

[eluser]Michael Wales[/eluser]
I have a list of 5 items, ordered from 1 (top) to 5 (bottom).

Moving an item 'up', from slot 3 to slot 2. Add one to the rank of all items between the old slot and the new slot, then update the moved item:
Code:
UPDATE items SET rank = rank + 1 WHERE rank < 3 AND rank >= 2
UPDATE items SET rank = 2 WHERE id = 375

Moving an item 'down', from slot 2 to slot 5. Subtract one from the rank of all items between the old slot and the new slot, then update the moved item:

Code:
UPDATE items SET rank = rank - 1 WHERE rank > 2 and rank <= 5
UPDATE items SET rank = 5 WHERE id = 375
#10

[eluser]ELRafael[/eluser]
the walesmd solution may work to you.
in that example, you'll set all that is high than the position to a down position

Rank | Position
&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1
&nbsp;&nbsp;&nbsp;B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2
&nbsp;&nbsp;&nbsp;C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3
&nbsp;&nbsp;&nbsp;D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4

If i wanna to put C in position 2, i'll put all that is less than 2 to original position + 1 and C in position 2.

Rank | Position
&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1
&nbsp;&nbsp;&nbsp;C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2
&nbsp;&nbsp;&nbsp;B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2(original_position)+1
&nbsp;&nbsp;&nbsp;D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3(original_position)+1

@elitemedia: are you france? you write like a (veeeery beautifull) friend mine from France.




Theme © iAndrew 2016 - Forum software by © MyBB