Fwd: Re[7]: [PHP] Help!! with array's Please
| From: | Tom Rogers | Date: | Sat, 01 Mar 2003 08:12:19 +0000 |
| Subject: | Fwd: Re[7]: [PHP] Help!! with array's Please | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-137675@lists.php.net to get a copy of this message | ||
This is a forwarded message
From: Tom Rogers <trogers@kwikin.com>
To: Richard Kurth <rdkurth@starband.net>
Date: Saturday, March 1, 2003, 5:59:11 PM
Subject: [PHP] Help!! with array's Please
===8<==============Original message text===============
Hi,
Saturday, March 1, 2003, 4:28:27 PM, you wrote:
RK> Hello Tom,
RK> I would love to see a basic class to do all of this. I took what you
RK> showed me and turned it into a function that works perfect.
RK> The one thing that I have had a lot of trouble with is manipulating
RK> text based data. And out of all the books I have, none of them get
RK> into doing this type of programing.
Here it is
class groupClass {
var $groups = array();
var $gid = 499; //if there are no groups, new ones start at this number +1
function groupClass($file){
$r = false;
if(file_exists($file)){
$in = file($file);
//build an array using names as keys so we can identify entries easier
while(list($key,$val) = each($in)){
$val = trim($val); //get rid of newlines
list($name,$pass,$gid,$userlist) = split (":", $val);
if($gid > $this->gid){
$this->gid = $gid; //keep track of the
highest gid number
}
$this->groups[$name]['gid'] = $gid;
$this->groups[$name]['pass'] = $pass;
$users = explode(',',$userlist); //get an array of
users
while(list($key2,$user) = each($users)){
// build an array using name as key
$this->groups[$name]['users'][$user] = 1;
}
}
$r = true;
//debug
//echo '<pre>';
//print_r($this->groups);
//echo '</pre>';
}
Return $r;
}
function addGroup($group,$gid = 0,$pass = 'x'){
$r = false;
if(!isset($this->groups[$group])){
$gid = ($gid > 0)? $gid : $this->gid +1; // if no gid then
generate the next highest
$this->groups[$group]['gid'] = $gid;
$this->groups[$group]['pass'] = $pass;
$r = true;
}
Return $r;
}
function delGroup($group){
$r = false;
if(isset($this->groups[$group])){
unset($this->groups[$group]);
$r = true;
}
Return $r;
}
function addUser($group,$user){
$r = false;
if(isset($this->groups[$group])){
$this->groups[$group]['users'][$user] = 1;
$r = true;
}
Return $r;
}
function delUser($group,$user){
$r = false;
if(isset($this->groups[$group]['users'][$user])){
unset($this->groups[$group]['users'][$user]);
$r = true;
}
Return $r;
}
function dump($file){
$r = false;
if($handle = fopen($file,'w')){
$groups = $this->groups; //get a copy of the groups array (saves
typing :)
//debug
//echo '<pre>';
//print_r($this->groups);
//echo '</pre>';
while(list($key,$val) = each($groups)){ //go through the groups
$users = $groups[$key]['users'];
//get a copy of the users array
$x = 0;
$userlist = '';
while(list($key2,$val2) = each($users)){ // add the users
$userlist .= ($x > 0)? ','.$key2 : $key2; //
don't need a comma for the first run
$x++;
}
$gid = $groups[$key]['gid'];
$pass = $groups[$key]['pass'];
$list =
$key.':'.$pass.':'.$gid.':'.$userlist."\n"; // make the
entry
fputs($handle,$list); //save it
}
$r = true;
fclose($handle);
}
Return $r;
}
}
//usage
$g = new groupClass('group');
$g ->addGroup('site8');
$g ->addUser('site8','fred');
$g->delUser('site7','tester7');
$g->dump('group2');
--
regards,
Tom
===8<===========End of original message text===========
--
regards,
Tom mailto:trogers@kwikin.com