Page 1 of 1

Can't get head arround this array.....

Posted: Tue Oct 30, 2007 11:21 pm
by Mr Tech
I am creating a script where you can upload files... You then choose what users get access to what files.

Here's an example of the code...

Code: Select all

File1.jpg

Select the users that have access to the above file:
<input type="checkbox" name="access[file1]" value="user1"> Ben 
<input type="checkbox" name="access[file1]" value="user2"> Peter

File2.jpg

Select the users that have access to the above file:
<input type="checkbox" name="access[file2]" value="user1"> Ben 
<input type="checkbox" name="access[file2]" value="user2"> Peter

File3.jpg

Select the users that have access to the above file:
<input type="checkbox" name="access[file3]" value="user1"> Ben 
<input type="checkbox" name="access[file3]" value="user2"> Peter
I then use the foreach function to pull that data.

The problem is, if I check both the access[file*] check boxes, because they are named the same, it only reads the second value... If I select both the access[file*] check boxes, I need it to give me the two I've selected for the same file.

Am I making sense?

I've tried a number of ways of laying the form fields out but none seem to give me my desired result... Maybe I'm doing something wrong in my foreach functoin

Code: Select all

<input type="checkbox" name="access[file3][user1]" value="user1">
<input type="checkbox" name="access[user1][file3]" value="user1">
<input type="checkbox" name="access[user1]" value="[file3]">
Here is an example of my foreach function:

Code: Select all

foreach ($_POST[fileaccess] as $user_id => $file_id) {
		echo "$user_id --- $file_id<br>";
	}

Posted: Tue Oct 30, 2007 11:47 pm
by s.dot
You're naming your arrays all wrong.

Code: Select all

<input type="checkbox" name="access[file1][]" value="user1">
<input type="checkbox" name="access[file1][]" value="user2">
<input type="checkbox" name="access[file1][]" value="user3">
Each name for the same file is always the same. It's the value that changes.

Then you could:

Code: Select all

if (!empty($_POST['access']['file1']))
{
    foreach ($_POST['access']['file1'] AS $file1Access)
    {
        echo 'file1 has access granted to ' . $file1Access . '<br />';
    }
}

Posted: Tue Oct 30, 2007 11:58 pm
by Mr Tech
Mate, you're a legend! I'll give it a go 8)