blargalarg Posted August 27, 2009 Share Posted August 27, 2009 Hi all! I've been writing PHP code for quite a few years now, but I think I've just gotten in over my head. I'm a decent programmer, but I'm by no means a master. I'm trying to write some code that will allow a user to arrange a group of links by their own preferance. The links are stored in a database, and I'd like to store a numerical value in the database as well to sort the links when the user views them (ie: link 1 goes at the top, link 2 underneath that, etc...). Here's my problem: I'd like to make it as easy as possible on the user. If the user decides to move 'link 4' to the 'link 1' position I'd like the code to auto-adjust all the way down - filling the gaps - and then storing the new values in the database. I'd guess my core question would be: How do I go about modifying the array to auto-arrange numbers and fill any gaps? Any help, pointers or tips would be greatly appreciated. Thank you for reading this! -Jack Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/ Share on other sites More sharing options...
mikesta707 Posted August 27, 2009 Share Posted August 27, 2009 Ok, i created a function that will add a specific key to any position on the array, and sort it accordingly. I believe its what you are asking for. the function is below function putPosition($arr, $pos, $key){ $newarr = array(); $added = false; foreach($arr as $k=>$v){ if ($k == $pos){//if we are at the correct key, add the value $newarr[] = $arr[$key]; $added = true;//verify that the key key has been added in its place } if ($added){//check if its been added //if so check if we are on its original key if($k == $key){ //if so, continue continue; } else { $newarr[] = $v; } } else { $newarr[] = $v; } } return $newarr; } the logic is pretty simple here are some test runs I did $array = array("one", "two", "three", "four", "five", "six", ">9000", "eight", "nine", "ten"); print_r($array); echo "<br />"; print_r(putPosition($array, 0, 3)); echo "<br />"; print_r(putPosition($array, 1, 3)); With the following results in order Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => >9000 [7] => eight [8] => nine [9] => ten ) Array ( [0] => four [1] => one [2] => two [3] => three [4] => five [5] => six [6] => >9000 [7] => eight [8] => nine [9] => ten ) Array ( [0] => one [1] => four [2] => two [3] => three [4] => five [5] => six [6] => >9000 [7] => eight [8] => nine [9] => ten ) Is that sort of what you asked for? Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-907890 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Why make it so hard? You can keep it as simple as it just is (this requires javascript for the drag-and-drop). I roll out (using PHP): <ul> <li><a href="http://link1">mylink #{$pos}</a> <input type="hidden" name="links[]"></li> <li><a href="http://link2">mylink #{$pos}</a> <input type="hidden" name="links[]"></li> <li><a href="http://link3">mylink #{$pos}</a> <input type="hidden" name="links[]"></li> <li><a href="http://link4">mylink #{$pos}</a> <input type="hidden" name="links[]"></li> <li><a href="http://link5">mylink #{$pos}</a> <input type="hidden" name="links[]"></li> </ul> Then the user modifies these by dragging and dropping them into the new order: <ul> <li><a href="http://link1">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link3">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link4">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link2">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link5">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> </ul> Then I use: $links = $_POST['links']; $sizeof = sizeof($links); for ($i = 0; $i < $sizeof; ++$i) { $id = $links[$i]; $query = "UPDATE links SET position = $i WHERE id = $id"; //.. } I roll out just like I did before: <ul> <li><a href="http://link1">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link3">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link4">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link2">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> <li><a href="http://link5">mylink #{$pos}</a> <input type="hidden" name="links[]" value="{$id}"></li> </ul> The links in the new order. Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-907897 Share on other sites More sharing options...
blargalarg Posted August 27, 2009 Author Share Posted August 27, 2009 Mike: My meagre skills tell me that your function would work for me, but what would be the best way to go about letting the user choose the link positions? A drop-down menu next to each link? I'm just trying to get my head around how to make the whole package work. ignace: I know nothing about Java - My programming skills are purely server-side. How exactly would I go about allowing the user to choose the position of the links? Is there some sort of java snippet out there that would interface and relay the new positional data to the php script? Thanks for the super-fast replies, guys. I really, really appreciate the help! -Jack Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-907905 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 It's not Java but JavaScript like PHP it's a scripting language (meaning it's syntax is interpreted and not translated like Java or C). You can find a few js scripts even frameworks (like jQuery) that allow you to re-order list items. Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-907913 Share on other sites More sharing options...
blargalarg Posted August 28, 2009 Author Share Posted August 28, 2009 ignace: So you're saying the answer is javascript and not PHP? Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-908036 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 So you're saying the answer is javascript and not PHP? No. I'm saying Java is not JavaScript their is a distinct difference between both. I found a tutorial that does what you want: http://www.wil-linssen.com/jquery-sortable-lists-with-drag-drop-handle/ Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-908179 Share on other sites More sharing options...
blargalarg Posted August 28, 2009 Author Share Posted August 28, 2009 Thank you very much for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/172189-assigningarranging-relational-numbers-to-an-array/#findComment-908500 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.