Can't get head arround this 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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

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

Post 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>";
	}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 />';
    }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Mate, you're a legend! I'll give it a go 8)
Post Reply