Jump to content

Adding id's to Array?


Monkuar

Recommended Posts

Okay, my brain is dumbfounded right now.

 

Let's say I want to store these id's in a array in a row in my db.  (Row name is "lol")

 

 

lol current value = "5,2,1"

 

now, let's say these are the id's of a user's friend list.  The user wants to add a new friend, their user id is "23"

 

How do I add "23" into the lol value "5,2,1" dynamically?

 

 

I know one way is to select the lol column data and just add a ",$data" but that is not dynamic and what if the user want's to delete the user id "23" after they added that user as a friend?

 

Thank you

 

 

edit: 600th Post on the spot baby!

Link to comment
Share on other sites

Hmm. Am I right in thinking you are storing the friend IDs all in the same value, deprecated by commas. I don't think that this is the best method to store friends.

 

The only way I can think, without redesigning the database, would be to (as you said) retrieve the list of friend IDs from the database and append the new one to the end.

 

When deleting a friend you could cycle thought each ID in the string and place them in an array. Look for the value of the ID to be removed within the array, remove it. Then insert the newly amended array back into the database.

Link to comment
Share on other sites

I highly suggest redesigning your database.

 

Create a new table called friends, with at least 2 columns (userId, friendId). Each row will store the ID of the user, and the ID of one of their friends.

 

You can then SELECT friendId WHERE userId = $currentuser

 

Or if you want the friends names, you can

SELECT users.fullName
FROM friends
LEFT JOIN users ON friends.friendId = users.id
WHERE friends.userId = $currentuser

Link to comment
Share on other sites

Xyph's solution is the right one.

 

The answer to your direct question is the explode() and implode() pair of functions.

 

However, redesigning your database now will save you lots of trouble in the future.  Your data model is wrong.

Link to comment
Share on other sites

Thanks all, was just using the friends system as an example.  I actually use xyph's method on my forum.

 

 

I was just thinking, let's say a user want's to block people from viewing his topic.  This user needs to click on a link to block such a user, such will indeed add that user's id to a column.

 

 

Joe = 23

Nick = 52

Rascal = 29

 

 

Joe made a topic, wants to block nick and rascal from viewing, their id's are 52,29 respectfully. 

 

Joe blocks nick by adding the 52 to the "block" column.  Now Joe want's to block rascal with the id of 29?  I need to add 29 into the "block" column.  That's easy, the problem is........... the block column now has "52,29" in it.  Now Joe want's to remove Nick from the blocked column. (Wants to remove 52 id) how do I do that dynamically?  Creating a whole other table for this, would be a scapegoat out of performance, their has to be a similar way by just using 1 column with a dynamical id array.

 

Then again, this would be a bitch to check if that id was already inserted....  Would have to explode/foreach the column id's like a bitch too..

 

Hmmm maybe the whole table idea is the best way.

Link to comment
Share on other sites

It would Definently be better to think about recreating the database/table. If you are wanting to black list people for viewing another users  content. A similar solution to xyph's friend solution would do the trick. Depending if you are wanting a user wide block or just for certain topics. If you are blacklisting users from viewing all a users content. Past and future. Then the table will just need the users I'd and the blocked users ID. If you are blocking only for individual posts etc, you will need a column for the ID of the post instead of the user who is creating the block. So each table only needs a min of 2 columns.

Link to comment
Share on other sites

Wait...why do you say you use xyph's method?  Xyph's method is multiple tables.

 

You cannot dynamically remove an item from a delimited list stored in a database column.  you have to pull the data out of the row into PHP, explode the list into an array, search the array for the item to remove, remove it, implode the list, and then update the database table.

 

Or, as everyone has suggested, you could make your database design correct, because that's how you avoid this problem.

Link to comment
Share on other sites

USER_ID   ||   FRIEND_ID
=========================
6         ||   1
=========================
1         ||   6
=========================
6         ||   22
=========================

 

The above is an example of the friend table that has been suggested your create. User 6 and 1 are friends with each other as they both have each other stored as a friend_id. user 6 is not friends with user 22 as user 22 does not have user 6 stored as a friend_id, this could be due to user 22 not hvaing accepted the request yet.

 

A blacklist table would be similar except it would have blacklisted_user_id instead of friend_id.

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.