Jump to content

Assigning/arranging relational numbers to an array


blargalarg

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.