Friends Arrays

Comments? Suggestions? Questions? If you have any about tsn, post them here.

Friends Arrays

Postby Neo » 24 Mar 2006, 15:32

Say you have 10 friends, and you want to have a top 5, but you want it orederable.

you have picture objects that have these fields, where the object name is budpic:

array budpic[] = 10

inside each budpic[id] would be this, where id = 0-9 :

budpic[id].previous
budpic[id].position
budpic[id].next

and you have these buddy pictures:
budpic[0]
budpic[1]
...
budpic[9]

The value of any given buddypic is this (for example budpic[4])

budpic[4].previous == 4
budpic[4].position == 5
budpic[4].next == 6

You'd use a link to move the pic to the right or left with the programming doing this:

function moveLeft(budpic, id) {
[tab]if ( onMouseClick=position.left )
[tab]{
[tab][tab]var picPrevious = budpic[id].previous[tab]//stores 3
[tab][tab]var picNext = budpic[id].next[tab]//stores 5
[tab][tab]var picPosition = budpic[id].position[tab]//stores 4

[tab][tab]set budpic[id].next = budpic[id].previous[tab]//sets .next to 3
[tab][tab]set budpic[id].previous = budpic[picPrevious].previous[tab]//sets .previous to the previous pic's .previous, in this case 2
[tab][tab]set budpic[id].position --[tab]//sets .position -1 to 3
[tab][tab]set budpic[picPrevious].position ++[tab]//sets the previous pic's .postion +1 to 4
[tab][tab]set budpic[picPrevious].next = picNext[tab]//sets previous pic's .next to value stored in picNext, 5
[tab][tab]set budpic[picPrevious].previous = id[tab]//sets previous pic's .previous to the pic we've been moving, 4
}


That's a complicated process, and it's much easier if you're adding a pic in that has not previously been in the array.

Another method is to keep track of all the buddy IDs and their position, and their user_id that is their buddy. Then to pull out all buddies for user_id 2, sort by position ascending, and then find the pic id that is one position lower, ++ it, -- your position, and store it to the database. (Of course that is the simple statement of how to do it...it's actually a bit more complicated.)
[[Neo]]
"Because I choose to."

Return to “the-spot.net C.S.Q.”