Jump to content

Move list item to Top


thirteen13

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.