Click to See Complete Forum and Search --> : point me to more efficient code
thorpe
04-06-2005, 02:34 AM
ok, just wondering how some of you guys might achieve the same results.
this function parses a 3 digit number, where no digit can be higher than 5. the 1st digit is an execute level, 2nd is read, and 3rd is write.
this allows me to set different read/write (im not using execute yet) levels for articles in my cms.
EXAMPLE:
$user_perms = permisions(505);
$article_perms = permisions(555);
if ($user_perms['read'] < $article_perms['read']) {
echo 'you dont have permision to view this article';
} else {
echo 'here is the requested article';
}
and here is the function.
function permisions($perms)
{
$arr = str_split($perms);
if (strlen($perms) < 3) {
for ($i=0;$i<count($arr);$i++) {
if ($arr[$i] > 5) {
$arr[$i] = 5;
}
}
$return['exec'] = 0;
$return['read'] = $arr[0];
$return['write'] = $arr[1];
} else {
for ($i=0;$i<count($arr);$i++) {
if ($arr[$i] > 5) {
$arr[$i] = 5;
}
}
$return['exec'] = $arr[0];
$return['read'] = $arr[1];
$return['write'] = $arr[2];
}
return $return;
}
it works.... just looking for ideas.
mrhappiness
04-06-2005, 03:36 AM
function permissions($perm) {
$result['read'] = $result['write'] = $result['exec'] = 0;
$perm = str_pad($perm, 3, '0', STR_PAD_LEFT);
for ($i = 0; $i < 3; $i++)
$perm{$i} = min(5, max(0, $perm{$i}));
$result['exec'] = $perm{0};
$result['read'] = $perm{1};
$result['write'] = $perm{2};
return $result;
}btw: you should pass strings to this function as 055 is interpreted as octal which is 45 decimal
thorpe
04-06-2005, 09:17 AM
ok... firstly, thanks a bunch for taking a look. im just starting out so sometimes its hard to see the quicker ways of doing things.
i tried your function a few times with a few different numbers passed to it. sometimes im getting some odd results though.
example;
$perms = permissions("045");
foreach ($perms as $val) {
echo $val;
}
returns;
054
because i dont completely understand your function i have no idea how to fix this. as you can see, i passed the function a string as you suggested. even when i tried typecasting i get the same result.
eg;
$p = (string) "045";
$perms = permissions($p);
foreach ($perms as $val) {
echo $val;
}
one more question. i am retrieving these numbers from a database field of type (int), is there a way to treat these numbers as string as you suggested?
im fairly sure even my function expected strings.
thorpe
04-06-2005, 09:28 AM
alrighty... i managed to incorperate some of your code, into some of mine, and i have a working version.
function permisions($perms) {
$arr = str_split(str_pad($perms,3,'0',STR_PAD_LEFT));
for ($i=0;$i<3;$i++) {
$arr[$i] = min(5, max(0, $arr[$i]));
}
$return['exec'] = $arr[0];
$return['read'] = $arr[1];
$return['write'] = $arr[2];
return $return;
}
thanks again...
Weedpacket
04-06-2005, 10:11 AM
Originally posted by thorpe
i tried your function a few times with a few different numbers passed to it. sometimes im getting some odd results though.
returns;
054
because i dont completely understand your function i have no idea how to fix this. That's because the function returns an associative array; if you were to look at the keys as well, you'd see they were paired up properly. What happened is that $result['exec'] was initialised first (to zero), then $result['write'] was initialised, then $result['read']. If the first line read
$result['write'] = $result['read'] = $result['exec'] = 0; then just printing out values via foreach() would output them in the same order. But still, as long as the keys are paired with the right values it shouldn't matter.
But it doesn't matter anyway, since those values are never even used.
I'd get rid of the loop and extra variables.
function permissions($perm) {
$perm = str_pad($perm, 3, '0', STR_PAD_LEFT);
return array(
'exec' => min(5,max($perm{0})),
'read' => min(5,max($perm{1})),
'write' => min(5,max($perm{2})),
);
}
Getting them from a database? Yup, should be fine; just as long as you don't do something literal like permissions(055) in your code. As mrhappiness said, that's equivalent to permissions(45).
thorpe
04-06-2005, 11:38 AM
cool!... and thankyou, never thought that my foreach could be the problem. didn't even think about it. your code is so much simpler than my original. and now with no loop too, thats got to be more efficient.
once again... thanks to you both.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.