Jump to content

Where should I start.


Aureole

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. ;D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!

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.