Aureole Posted December 17, 2007 Share Posted December 17, 2007 I have in my database a table called "groups", inside this table is a field called "members". Then for each group, for the members I store them as such: 1|87|38|5|25 Each one of those numbers is a member id. I then use explode() to put them into an array so I can use 'em. Now here's my problem, I need a way to remove members. Say Jim whose member id is 38 wants to leave the group. So I would need to turn "1|87|38|5|25" into "1|87|5|25". I really have no idea how to approach this. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 17, 2007 Share Posted December 17, 2007 If you're asking "How would I do this in SQL", the answer is "With great difficulty". That data structure is just not suited to SQL manipulation. But you can easily read the row into PHP, explode it into an array, remove the offending member id, implode it and store it again. Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 17, 2007 Author Share Posted December 17, 2007 Oh ok. Would it be possible for someone to give me a rough idea of how to do this, once I see the code then I'll know how to do it next time. I think that's just how I learn. I'll try and work it out by myself of course, but I'd much prefer save myself a few Hours of messing around trying to figure something out. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 17, 2007 Share Posted December 17, 2007 Why not redistribute your data with php! you make a second table called Groups_Users and you do this UserID GroupID Status and basically you do this <?php //Open Sql connection $q = "Select GroupID, members, from `groups`"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ while($row = mysql_fetch_array($r)){ $temp = explode("|",$row['members']); foreach($temp as $val){ $tempq = "Insert into `Groups_Users` (GroupID,MemberID) VALUES('".$row['GroupID']."', '".$val."')"; $tempr = mysql_query($tempq) or die(mysql_Error()); } } } ?> Then you got a valid mysql way to handle users so when you remove a user you say <?php $q = "Delete from `Groups_Users` Where GroupID = '".$groupid."' and UserID = '".UserID."'"; ?> A lot easier to delete, premote, demote, etc. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 17, 2007 Share Posted December 17, 2007 It sounds like you've already got code to create the member list, so I'll just give the specific stuff. $members_sql = '1|87|38|5|25'; # Original list from SQL $members_arr = explode('|', $members_sql); # The exploded member list $members_arr_new = array_diff($members_arr, array('38')); # Remove member 38 $members_sql_new = implode('|', $members_arr_new); # Ready to go back into SQL There are many other ways to remove an element from an array, but I think array_diff() is the simplest. Quote Link to comment Share on other sites More sharing options...
Aureole Posted December 17, 2007 Author Share Posted December 17, 2007 It sounds like you've already got code to create the member list, so I'll just give the specific stuff. $members_sql = '1|87|38|5|25'; # Original list from SQL $members_arr = explode('|', $members_sql); # The exploded member list $members_arr_new = array_diff($members_arr, array('38')); # Remove member 38 $members_sql_new = implode('|', $members_arr_new); # Ready to go back into SQL There are many other ways to remove an element from an array, but I think array_diff() is the simplest. That works perfectly, thanks a lot! Quote Link to comment 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.