salasis Posted March 23, 2006 Share Posted March 23, 2006 Hi there,It may seem a bit vaque but i would be REALLY grateful if you could help me in this.I would like to be able to set the order in which results display:e.g.Result 1Result 2Result 3Result 4Result 5I would like that so i could decide in which order it displays.e.g so i could do this:Result 4Result 2Result 5Result 1Result 3So i know its basically just adding another field and caling it something like "order" and just updating it to what i want.BUT my question is....If the table stands as this:[b]Name:[/b] Result 1 [b]Ord:[/b] 4[b]Name:[/b] Result 2 [b]Ord:[/b] 3[b]Name:[/b] Result 3 [b]Ord:[/b] 2[b]Name:[/b] Result 4 [b]Ord:[/b] 1[b]Name:[/b] Result 5 [b]Ord:[/b] 5(So i would just use [i]SELECT * FROM `table` ORDER BY `Ord` DESC[/i])But when i update it say, using a form. The values are gona conflict...(if that makes sence).E.gWhen i change [b]Result 3[/b] to have a [b]Ord[/b] value of [b]1[/b], Result 4, will already have a Ord value of 1.So both need to change, but after 4 changes, another one will have to change etc etc....Please help if you can, thankyou!. Quote Link to comment https://forums.phpfreaks.com/topic/5622-ordering-listsresults/ Share on other sites More sharing options...
wickning1 Posted March 23, 2006 Share Posted March 23, 2006 It depends on whether you move something up or down.If you move it up, then everything from the new value to the old value need to shift +1. If you move down, things need to shift -1.This can be done in three queries: (this is pseudocode)[code]SELECT (`Ord` - newpos) as range FROM table WHERE name='nametomove'IF range > 0 THENUPDATE table SET `Ord`=`Ord`+1 WHERE `Ord` BETWEEN newpos AND newpos+rangeELSEUPDATE table SET `Ord`=`Ord`-1 WHERE `Ord` BETWEEN newpos+range AND newposEND IFUPDATE table SET `Ord`=newpos WHERE name='nametomove'[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5622-ordering-listsresults/#findComment-20107 Share on other sites More sharing options...
salasis Posted March 23, 2006 Author Share Posted March 23, 2006 Thanks, but thats kinda confusing lol....I found this on the internet:[code] function processMoviesOrder($key) { if (!isset($_POST[$key]) || !is_array($_POST[$key])) return; $movies = getMovies(); $queries = array(); $ranking = 1; foreach ($_POST[$key] as $movie_id) { if (!array_key_exists($movie_id, $movies)) continue; $query = sprintf('update movies set ranking = %d where movie_id = %d', $ranking, $movie_id); mysql_query($query); $ranking++; } }[/code]But its confusing as well lol. Quote Link to comment https://forums.phpfreaks.com/topic/5622-ordering-listsresults/#findComment-20118 Share on other sites More sharing options...
wickning1 Posted March 24, 2006 Share Posted March 24, 2006 Ok, let's say you have 5 people in the database and you want to order them as you described. The database would start like this:[code]Mark 1John 2Susan 3Nick 4Zach 5[/code]Now let's say you want to move Nick to position 2:[code]Mark 1Nick 4John 2Susan 3Zach 5[/code]Mark and Zach still have the correct number, because they were not between 4 and 2, but everyone between 4 and 2 needs 1 added.[code]Mark 1Nick 5John 3Susan 4Zach 5[/code]Now we just set Nick to 2, like we intended:[code]Mark 1Nick 2John 3Susan 4Zach 5[/code]The code you use to do all this is what I posted earlier. Hopefully that will get you started. Quote Link to comment https://forums.phpfreaks.com/topic/5622-ordering-listsresults/#findComment-20177 Share on other sites More sharing options...
salasis Posted March 24, 2006 Author Share Posted March 24, 2006 Ahhh, i see, thankyou very much :D Quote Link to comment https://forums.phpfreaks.com/topic/5622-ordering-listsresults/#findComment-20247 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.