Jump to content

IF 2 arrays are true


Boxerman
Go to solution Solved by jcbones,

Recommended Posts

Hey guys,

 

Hopefully a quick one for you...

 

I'm trying to check if two arrays match then output text.

 

what i have is: 

if (in_array($usernamelower, $superusers AND $adminusers, true)) {

full code: 

//Let's see who has what access
$adminsql = "SELECT * FROM permissions WHERE level = 'Admin' ";
$superusersql = "SELECT * FROM permissions WHERE level = 'Super User' ";
$adminresult = mysql_query($adminsql) or die('Query failed: ' . mysql_error()); 
$superuserresult = mysql_query($superusersql) or die('Query failed: ' . mysql_error()); 
//Let's build the adminuser array
while($adminrow = mysql_fetch_assoc($adminresult)){ 
    $adminusers[] =$adminrow['username']; 
}  
//Let's build the superuser array
while($superuserrow = mysql_fetch_assoc($superuserresult)){ 
    $superusers[] =$superuserrow['username']; 
} 

the if(in array) doesnt work :( can anyone help with this one.. i've tried AND, ||,  && 

Link to comment
Share on other sites

Barand, I'm guessing that he has the usernames multiple times in the permissions table, for the different levels.  It is a bad design with bad logic, but with the example that the OP put forward, this is how it looks to be.

Link to comment
Share on other sites

Do you mean the table would look like this, where user A is both Admin and Super User?

mysql> SELECT * FROM permissions;
+----+----------+------------+
| id | username | level      |
+----+----------+------------+
|  1 | A        | Admin      | *
|  2 | B        | Moderator  |
|  3 | C        | Admin      |
|  4 | D        | Super User |
|  5 | E        | Moderator  |
|  6 | A        | Super User | *
+----+----------+------------+

then

WHERE level = 'Admin' AND level = 'Super User'

will not find any records as there is no single record that has both values at the same time (impossible)

mysql> SELECT username
    -> FROM permissions
    -> WHERE level = 'Admin' AND level = 'Super User';
Empty set (0.00 sec)

Using OR instead of AND would produce

mysql> SELECT username
    -> FROM permissions
    -> WHERE level = 'Admin' OR level = 'Super User';
+----------+
| username |
+----------+
| A        |
| C        |
| D        |
| A        |
+----------+

However, to find only users with both

mysql> SELECT A.username
    -> FROM permissions A
    -> INNER JOIN
    ->     permissions B
    ->     ON A.username = B.username
    ->     AND A.level = 'Admin'
    ->     AND B.level='Super User';
+----------+
| username |
+----------+
| A        |
+----------+

Back to the original question. From the two arrays, $admins and $superusers, to find who is in both arrays use array_intersect()

$admins = array ('A', 'C');
$superusers = array ('D', 'A');

$both = array_intersect($admins, $superusers);

echo '<pre>',print_r($both, true),'</pre>';

/** RESULT *****

    Array
    (
        [0] => A
    )

****************/
Link to comment
Share on other sites

If the levels are a single value do it in one loop and make arrays comparing the levels

$adminusers = array();

$superusers = array();

$sql = "SELECT (username,level) FROM permissions WHERE level = 'Admin' OR level = 'Super User' ";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
    if($row['level'] == 'Admin'){
        $adminusers[] = $row['username']; 
    }else{
        $superusers[] = $row['username']; 
    }
} 

Or if wanted to get all users privileges

$adminusers = array();
$superusers = array();
$users = array();

$sql = "SELECT (username,level) FROM permissions";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
    if($row['level'] == 'Admin'){
        $adminusers[] = $row['username'];
    }elseif($row['level'] == 'Super User'){
        $superusers[] = $row['username'];
    }else{
        $users[] = $row['username'];
   }
} 

Now this would be ok if wanted a huge list, I personally like to just check what their level is on login. Set in a session if matches admin or superuser. Otherwise no login or not matching those in session would be set as a normal user in session.

 

On the subject of naming levels it's easier to do a numbering one such as 1-10.

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.