thirteen13 Posted April 13, 2010 Share Posted April 13, 2010 I have the following PHP Code which queries a database and generates a list of 4 items. echo'<a href="index.php?id='.$id.'&p='.$row["id"].'"><li style="width:207px; background-color:#bcbec0; color:#ffffff; border:0px; font-weight:bold;" class="mainli">'.$row["pagetitle"].'</li></a>'; $sql2 = "Select * from $table where parent = $row[id] and published = 1 order by menuindex asc"; It works fine and as needed. What I need now is that when I click on a certain item, it moves to the top of the list and the rest remain as they are. For example it generates: Nokia Samsung Siemens Motorola When I lick on Siemens, the list is displayed as follows: Siemens Nokia Samsung Motorola Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/ Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 There are a few ways to do this. 1. Before the loop, check if the user clicked on anything. If so, display that one. Then in the loop, just check if the IDs match and exclude the echo for the menu item that the user clicked. 2. Before the loop, print out the user-clicked item. Then change the SQL so that it excludes the selected item. Then print the rest. Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040939 Share on other sites More sharing options...
thirteen13 Posted April 13, 2010 Author Share Posted April 13, 2010 I'm still a junior programmer in PHP, could you please refer me a demo example or code? Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040954 Share on other sites More sharing options...
aebstract Posted April 13, 2010 Share Posted April 13, 2010 Are you needing it to update your database so that the list is altered forever or just this one time that the user is there and only for that user? Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040959 Share on other sites More sharing options...
ChemicalBliss Posted April 13, 2010 Share Posted April 13, 2010 hmm, as simple as it sounds its not quite as simple, in fact it can be quite complicated, so here is how i would do it: <?php // Array Index [index], gets moved to top of [array]. function resort_top($index,$array){ $newarray = array($array[$index]); for($i=0;$i<count($array);$i++){ if($i == $index){ // skip }else{ $newarray[] = $array[$i]; } } return $newarray; } $array = array('a','b','c','d'); $index = 0; // Starts at 0 print_r(resort_top($index,$array)); ?> Basically get the array index you want moved, start an array with that, then, add each array element in the order they were in, and skip the one that was moved. simple. -CB- Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040961 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 ChemicalBliss, depending on how the drop-down is generated, I suppose. If it's manually generated instead of a loop, then it gets tricky. If it's just a loop and the result is in the database or something, then it's easy. The only problem is that I don't know if this is dynamic. So the second someone clicks on the item, it gets moved to the top? That may require JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040964 Share on other sites More sharing options...
ChemicalBliss Posted April 13, 2010 Share Posted April 13, 2010 You will need to refresh the page to get the results, i would reccommend for easy sake, to store the array inside a session variable. -CB- Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040967 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 You will need to refresh the page to get the results, i would reccommend for easy sake, to store the array inside a session variable. -CB- What about AJAX? Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040971 Share on other sites More sharing options...
ChemicalBliss Posted April 13, 2010 Share Posted April 13, 2010 I was only infering my example in the most easy way. Yes if you want to use AJAX you can, it will save you bandwidth in the end, but thats beyond the scope of his question and would be advised to start a new thread if he has problems with that. -CB- Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040976 Share on other sites More sharing options...
thirteen13 Posted April 13, 2010 Author Share Posted April 13, 2010 Each list item is a hyperlink so it will load a new page with the same menu items. I need the previously clicked menu item to display on top of the menu when the menu is generated from the database. Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040981 Share on other sites More sharing options...
aebstract Posted April 13, 2010 Share Posted April 13, 2010 Are you needing it to update your database so that the list is altered forever or just this one time that the user is there and only for that user? Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040992 Share on other sites More sharing options...
thirteen13 Posted April 13, 2010 Author Share Posted April 13, 2010 I will re-explain. I have a side-menu generating the menu items from the database as seen in my first post. For example, the following menu items are generated: Nokia Siemens Samsung Motorola Each menu item has a link to a different page. If I press on Samsung, I want Samsung to go on the top of the menu list and the content loaded. Now the content loads all right. The thing which I cannot do is make the clicked menu item go on top of the list. Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1040994 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 We've already offered help on that issue. Read the first few replies in this topic. If you want further help, please post the pertinent code that's not working and we'll help you diagnose the issue. Quote Link to comment https://forums.phpfreaks.com/topic/198374-move-list-item-to-top/#findComment-1041032 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.