Monkuar Posted July 19, 2012 Share Posted July 19, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/ Share on other sites More sharing options...
mikhl Posted July 19, 2012 Share Posted July 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362651 Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362759 Share on other sites More sharing options...
mikhl Posted July 19, 2012 Share Posted July 19, 2012 I would also highly reccomend xyph's suggestions. This will be much easier in the long term. Your database will be more managable and user friendly. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362763 Share on other sites More sharing options...
ManiacDan Posted July 19, 2012 Share Posted July 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362764 Share on other sites More sharing options...
Jessica Posted July 19, 2012 Share Posted July 19, 2012 4thed. Fix it now, it'll be easier in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362768 Share on other sites More sharing options...
Monkuar Posted July 20, 2012 Author Share Posted July 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362953 Share on other sites More sharing options...
mikhl Posted July 20, 2012 Share Posted July 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1362959 Share on other sites More sharing options...
ManiacDan Posted July 20, 2012 Share Posted July 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1363035 Share on other sites More sharing options...
mikhl Posted July 20, 2012 Share Posted July 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265932-adding-ids-to-array/#findComment-1363052 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.