Hi,

Friday, February 28, 2003, 5:39:39 PM, you wrote:
RK> Hello Tom,

RK> Thursday, February 27, 2003, 7:39:52 PM, you wrote:

TR>> Hi,

TR>> Friday, February 28, 2003, 12:20:38 PM, you wrote:

RK>>> I really need somebody to help me with this I am totally lost on what
RK>>> to do

RK>>> I need a way to read the following text file and add to or delete from
RK>>> or change the data.
RK>>> I have been able to move it all into an array using this code
RK>>> $groups= file("group");
RK>>> for ($i=0; $i<$number_in_group; $i++){
RK>>> $groups[0];
RK>>> $groups[3]; <-- this is the part of the array that I need to change
RK>>> }
RK>>> But I can not figure out how to search the array and delete from or add
RK>>> to the array at a given point. I would like to say add another user to
RK>>> the end of site6 or delete a user from site3.
RK>>> Could somebody give me a hand here. I have read the manual for arrays
RK>>> and still can't figure it out.

RK>>> site1:x:503:tester1
RK>>> site2:x:504:tester2,tester2a
RK>>> site3:x:505:tester3,tester3a,tester3b
RK>>> site4:x:506:tester4
RK>>> site5:x:507:tester5,tester5a,tester5b
RK>>> site6:x:508:tester6
RK>>> site7:x:509:tester7,tester7a,tester7b  

RK>>> -- 
RK>>> Best regards,
RK>>>  Richard                          mailto:[EMAIL PROTECTED]

TR>> your groups array will look like this

TR>> groups[0] = site1:x:503:tester1
TR>> groups[1] = site2:x:504:tester2,tester2a
TR>> so loop through the array

TR>> $x = 0;
TR>> while(list($key,$val) = each($groups)){

TR>> //you need split the array values like this
TR>>       list($name,$pass,$gid,$user_list) = split (":", $groups[$x]);
TR>>       //see if we have the right one
TR>>       if($name = $wanted_name){ //site6
TR>>              //then split the usernames into a sub array
TR>>              $users = explode(',',$userlist);
TR>>              // to add
TR>>              $users[] = $newuser;
TR>>              // to delete
TR>>              unset($users[3])
TR>>              // now add it back
TR>>              $userlist = implode(',',$users)
TR>>              $list = $name.':'.$pass.':'.$gid.':'.$userlist;
TR>>              $groups[$x] = $list;
TR>>       }
TR>>       $x++;
TR>>       //here you could fputs to a temp file then copy to the original
TR>>       // after finished
TR>> }

TR>> Then write groups back to disk
TR>> -- 
TR>> regards,
TR>> Tom

RK> Thanks for the help!!

RK> I still can't seam to get it to add a new user at the end of a line of
RK> the selected site name.
RK> Also how do I use the unset($users[3]) do I first have to determine
RK> what number the one I what to delete is.
RK> Here is what I have so far
RK> $newuser="tester45";
RK> $wanted_name="site7";
RK> $groups= file("group");
RK> $x = 0;
RK> while(list($key,$val) = each($groups)){
RK> //you need split the array values like this
RK>  list($name,$pass,$gid,$user_list) = split (":", $groups[$x]);
RK>      //see if we have the right one
RK>          if($name = $wanted_name){ //site6
RK>         //then split the usernames into a sub array
RK>          $users = explode(',',$userlist);
RK>          // to add
RK>                  $users[] = $newuser;
RK>           // to delete
RK>          //unset($users[3]);
RK>          // now add it back
RK>          $userlist = implode(',',$users);
RK>          $list = $name.':'.$pass.':'.$gid.':'.$userlist;
RK>          $groups[$x] = $list;
RK>       }
          
RK>       $x++;
RK> $handle = fopen("group2", 'a');
RK> fputs($handle, $groups[$x]);
RK>           //here you could fputs to a temp file then copy to the original
RK>       // after finished
RK> }
RK> //Then write groups back to disk





RK> -- 
RK> Best regards,
RK>  Richard                            mailto:[EMAIL PROTECTED]

Here is a revised one with the bugs reduced :)

$newuser="tester45";
$del_user = "tester7";
$wanted_name="site7";
$groups= file("group");
$handle = fopen("group2", 'w');
$x = 0;
while(list($key,$val) = each($groups)){
//you need split the array values like this
        $val = trim($val);
        list($name,$pass,$gid,$userlist) = split (":", $val);
     //see if we have the right one
        if($name == $wanted_name){ //site6
                //then split the usernames into a sub array
                $users = explode(',',$userlist);
                // to add
                $users[] = $newuser;
                // to delete
                reset($users);
                while(list($key2,$val2) = each($users)){
                        echo 'user = '.$val2.'<br>';
                        if($val2 == $del_user){
                                echo 'Deleting '.$del_user.'<br>';
                                unset($users[$key2]);
                        }
                }
                // now add it back
                $userlist = implode(',',$users);
                echo 'User list = '.$userlist.'<br>';
                $list = $name.':'.$pass.':'.$gid.':'.$userlist;
                fputs($handle, $list."\n");     //add updated list
        }else{
                fputs($handle, $val."\n");      //put back original
        }
          
  $x++;
}
fclose($handle);

-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to