For Each on Array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

For Each on Array?

Post by sockpuppet »

Hi Guys,

I'm trying to implement user permissions. I have a site that will have user permissions set for each site.

so

$_SESSION['user_permissions']['new york']['edit_files'] = 1
$_SESSION['user_permissions']['new york']['admin_files'] = 1
$_SESSION['user_permissions']['boston']['edit_files'] = 1

Would be the output.

The user permissions are stored in a table where one row is equivalent to a location (e.g. new york or boston) and the admin_files / user_files are colums thus allowing me to expand the range of user permissions without too much coding. Now I know the code below is wrong as you can refer to $row inside the foreach but can anyone give me a pointer onto how to manage this?

Cheers
Richard

Code: Select all

			foreach ($row as $key => $value) {
				if($value > 0) {
					$_SESSION['user_Permissions'][$row['up_Site']][$key] = $value;
				}
			}

Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: For Each on Array?

Post by Peter Kelly »

I think you should replace foreach with while as such below

Code: Select all

$result = mysql_query("SELECT * FROM table");  
while($r=mysql_fetch_array($result)) 
{     

} 
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

Re: For Each on Array?

Post by sockpuppet »

I'm using a mysql wrapper that returns the results in the array $row.

I'd also like to use a foreach or similar so I only have to change the sql and the new user permission variable is in use I don't want to have to edit the php in 2 places.

I could just list all the columns manually e.g. $row['admin'], $row['user'] etc but that would be less than flexible should I change any of the user permission variables.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: For Each on Array?

Post by requinix »

What does

Code: Select all

print_r($row);
produce?
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

Re: For Each on Array?

Post by sockpuppet »

tasairis wrote:What does

Code: Select all

print_r($row);
produce?
Prints this....very basic at the moment as testing. up_Site is the site the permissions are set for. up_ anything else will be a permission.

Code: Select all

Array
(
    [0] => Array
        (
            [up_Site] => 1
            [up_ViewDrivers] => 1
            [up_EditDrivers] => 1
        )

    [1] => Array
        (
            [up_Site] => 2
            [up_ViewDrivers] => 1
            [up_EditDrivers] => 0
        )

)
sockpuppet
Forum Newbie
Posts: 22
Joined: Tue Jan 18, 2011 8:38 am

Re: For Each on Array?

Post by sockpuppet »

I changed it to this, arguably more secure - just takes more editing. Would be nice to sort it out automatically though as adding the sql should be secure enough and even if someone did manage to set a "new" permission I'd have to tell the site to actually do something with it first.

Code: Select all

		

$allowed_perms = array('up_EditDrivers','up_ViewDrivers');

				foreach($row as $record) {
				
					foreach($allowed_perms as $key => $value) {
					
						if($record[$value] > 0) {
							$_SESSION['user_Permissions'][$value][$record['up_Site']] = $record[$value];
						}
						
					}
				
				}
Post Reply