2

I'm trying to manipulate a text string based on values assigned to a user.

The FULL text string is 'Lists, Items, Cases, Identity and Location' and this is only shown in full against ADMIN.

Each user on the system has access rights. Each right determines what they can and can't do. So access right a1 = 'Lists', b1 = 'Items' etc...

When the user logs in the page shows what they have access to.

eg:

Welcome $user
You have access to $foo

for the admin user.. this would look like:

Welcome Admin
You have access to Lists, Items, Cases, Identity and Location

So now I'm trying to do the following :

if ($user == 'admin') $foo = 'Lists, Items, Cases, Identity and Location'; // full access

But I need to be able to change the value for each user.

$foo ='';
if (strpos($access,'a1') !== false) $foo .= 'Lists ';
if (strpos($access,'b1') !== false) $foo .= 'Items ';
if (strpos($access,'c1') !== false) $foo .= 'Cases ';
if (strpos($access,'d1') !== false) $foo .= 'Identity ';
if (strpos($access,'e1') !== false) $foo .= 'Location ';

The issue I have with this is the punctuation is messed up. The user Miz has access to a1,b1,c1

When she logs in she gets:

Welcome Miz
You have access to Lists Items Cases

What I'd like it to say is:

Welcome Miz
You have access to Lists, Items and Cases

Can anyone advise the best way to achieve this

Thanks

2
  • Use an array to save the list and print it by iterating over the array, you can apply the separator based on current item number .. Commented Dec 15, 2014 at 12:18
  • You could add an array with all the things an user can do. Like, lists, items, cases, identity, location. In that array also add the 'a1' as a key, or value. What ever you like. Loop over the array for the desired user, and fill these values into another array. Then simply use explode or implode ( no idea which one, I always forget that.. ) on the array and boom. You got the comma's, if you want to do the 'and' thing, loop over the array and see if it is the last one. If so, add " and " there, else ",". Commented Dec 15, 2014 at 12:20

3 Answers 3

3

Use implode()

<?php
$arr = array();

if (strpos($access,'a1') !== false) $arr[] =  'Lists ';
if (strpos($access,'b1') !== false) $arr[] =  'Items ';
if (strpos($access,'c1') !== false) $arr[] =  'Cases ';
if (strpos($access,'d1') !== false) $arr[] =  'Identity ';
if (strpos($access,'e1') !== false) $arr[] =  'Location ';
$last = array_pop($arr);
$foo = count($arr) ? implode(", ", $arr) . " AND " . $last : $last;
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Mostly right but it doesn't cover this : Lists, Items and Cases
@SyedQarib Check my answer for that.
@SyedQarib, thanks for the suggestion. It helped me a lot. I MacMan also deserve a +1 for asking this nice question.
0

Try to use an array and join:

$rights = array();
$foo = '';
if (strpos($access, 'a1') !== false)
    $rights[] = 'Lists ';
if (strpos($access, 'b1') !== false)
    $rights [] = 'Items ';
if (strpos($access, 'c1') !== false)
    $rights [] = 'Cases ';

if (strpos($access, 'd1') !== false && strpos($access, 'e1') !== false) {
    $rights[] = "Identity and Location";
} else {
    if (strpos($access, 'd1') !== false)
        $rights[] = 'Identity ';
    if (strpos($access, 'e1') !== false)
        $rights[] = 'Location ';
}
echo join(', ', $rights);

EDIT: Based on OP comment in other answer, Identity and Location handled.

Comments

0

Something like this will work.

$foo ='';
if (strpos($access,'a1') !== false) $foo .= 'Lists, ';
if (strpos($access,'b1') !== false) $foo .= 'Items, ';
if (strpos($access,'c1') !== false) $foo .= 'Cases, ';
if (strpos($access,'d1') !== false) $foo .= 'Identity, ';
if (strpos($access,'e1') !== false) $foo .= 'Location, ';

$str = rtrim($foo,', ');  //remove last ","
echo preg_replace('/,\s([^,]*$)/', ' and $1', $str);  //replace last ", " with " and "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.