ShoeLace1291 Posted January 23, 2010 Share Posted January 23, 2010 I'm making a gruop permissions form that loops through my categories and subcategories. It lists checkboxes for each subcategory which returns a value of true or false. Checking these boxes will determine if the members of the group that's being created will be allowed to perform certain actions(read, create, delete, etc) in the checkbox's category. I need to know how I should name my fields so I can loop through the post data for each category instead of each action. This is my form source(it's generated by a database). <table align='center' cellspacing='1' cellpadding='3' border='0'> <tr> <td align='right'>Category</td> <td align='center'>Read</td> <td align='center'>Create</td> <td align='center'>Modify</td> <td align='center'>Attach</td> <td align='center'>Comment</td> <td align='center'>Delete</td> </tr><tr> <td align='left' colspan='7' style='background: #e4e4e4;'>News</td> </tr><tr><td align='right'>Website</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='left' colspan='7' style='background: #e4e4e4;'>Reviews</td> </tr><tr><td align='right'>Xbox 360</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>PlayStation 3</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>Nintendo Wii</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>PC Games</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>Nintendo DS</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>PlayStation Portable</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='left' colspan='7' style='background: #e4e4e4;'>Tutorials</td> </tr><tr><td align='right'>HTML/CSS</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>PHP Coding</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>JavaScript</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> <td align='right'>Adobe PhotoShop</td> <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> </tr><tr> </tr> </table> I thought about just looping through each action but then I would have to insert rows into my permissions table and updating them for each action. It would be better to do it by subcategory. I think the main issue is what I should name my checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/ Share on other sites More sharing options...
laffin Posted January 23, 2010 Share Posted January 23, 2010 something to know about checkboxes: they dont return a name/value pair if not checked. So biggest problem with your current form is that you will have no idea which checkboxes belong to which category easiest solution, putting the id in the name as: <td align='center'><input type='checkbox' name='read[8]' id='read[]' value='1'></td> this way you can use foreach($array,$key=>$val), to retrive the id value stored as the array index value Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1000598 Share on other sites More sharing options...
ShoeLace1291 Posted January 24, 2010 Author Share Posted January 24, 2010 How would I loop through something like that to process the form? Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1000665 Share on other sites More sharing options...
ShoeLace1291 Posted January 30, 2010 Author Share Posted January 30, 2010 bump Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004075 Share on other sites More sharing options...
teamatomic Posted January 30, 2010 Share Posted January 30, 2010 Its an array, built by a script. Why not: name="read[$category][]" $read=$_POST['read'] foreach($read as $cat) { foreach($cat as $value) { echo "$cat::$value<br>"; } } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004078 Share on other sites More sharing options...
ShoeLace1291 Posted January 30, 2010 Author Share Posted January 30, 2010 something to know about checkboxes: they dont return a name/value pair if not checked. So biggest problem with your current form is that you will have no idea which checkboxes belong to which category easiest solution, putting the id in the name as: <td align='center'><input type='checkbox' name='read[8]' id='read[]' value='1'></td> this way you can use foreach($array,$key=>$val), to retrive the id value stored as the array index value So as far as the name read[8], is 8 equal to the unique id of the category? and that's an eight and not a zero right? Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004079 Share on other sites More sharing options...
ignace Posted January 30, 2010 Share Posted January 30, 2010 No that 8 is your primary ID to identify your database record So it should in fact be: <td align='center'><input type='checkbox' name='read[<?php print $row['id']; ?>]' value='1'></td> id='read[]' is invalid because it needs to be unique and as you are looping through an array it will show up multiple times making it not unique. Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004151 Share on other sites More sharing options...
ShoeLace1291 Posted February 1, 2010 Author Share Posted February 1, 2010 The problem with your method is that it will loop through only by the action. I need to loop through by category and then get the value of the action according to the category it's in. Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004859 Share on other sites More sharing options...
laffin Posted February 1, 2010 Share Posted February 1, 2010 Ignace's statement id='read[]' is invalid because it needs to be unique and as you are looping through an array it will show up multiple times making it not unique. should require some clarification read[] is valid, it will just assign they key index to the next available numeric sequence. But if all the values are 1 there is no way to differentiate for which id. So u need the array to either have a unique key index or a unique value to be able to differentiate. So its a question of, How do I pass an id from multiple rows of a table. and the answer really depends on how many values are possible from the form if all the checkbox values are the same, 1 u can easily use <td align='center'><input type='checkbox' name='read[]' value='<?php print $row['id']; ?>'></td> which can be retrived with foreach($_POST['read'] as $id) echo "Read Category $id selected"; if u plan on using a variety of values than <td align='center'><input type='checkbox' name='read[<?php print $row['id']; ?>]' value='1'></td> which can be retrieved with foreach($_POST['read'] as $id=>$val) { echo "Read Category $id selected with value: '{$val}'"); } The problem with your method is that it will loop through only by the action. I need to loop through by category and then get the value of the action according to the category it's in. It wasnt our method, that was the intitial method you posted. So you have to rethink how you want your POST variables set in order to process them by id's instead of actions lets take a look at your form <td align='center'><input type='checkbox' name='read[]' id='read[]' value='1'></td> <td align='center'><input type='checkbox' name='create[]' id='create[]' value='1'></td> <td align='center'><input type='checkbox' name='modify[]' id='modify[]' value='1'></td> <td align='center'><input type='checkbox' name='attach[]' id='attach[]' value='1'></td> <td align='center'><input type='checkbox' name='comment[]' id='comment[]' value='1'></td> <td align='center'><input type='checkbox' name='delete[]' id='delete[]' value='1'></td> Now ask yourself, how can I deliver this content by category instead of an action. simple answer, assign actions as a value instead of a variable name <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='read'></td> <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='create'></td> <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='modify'></td> <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='attach'></td> <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='comment'></td> <td align='center'><input type='checkbox' name='action[<?php print $row['id']; ?>][]' value='delete'></td> Now see the difference, I categorized an action array based on the id. which contains an array of which actions were chosen. foreach($_POST['action'] as $id=>$actions) { echo "Read Category {$id} selected with actions: '"; foreach($actions as $action) echo "{$action}"; echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1004896 Share on other sites More sharing options...
ShoeLace1291 Posted February 3, 2010 Author Share Posted February 3, 2010 It's giving me an invalid argument error for the second foreach statement. Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1005916 Share on other sites More sharing options...
ShoeLace1291 Posted February 5, 2010 Author Share Posted February 5, 2010 I think I've figured out what to name it. It needed to be named action[$id][] with the value set to whatever the action is. The only problem that remains is that the values of unchecked boxes need to be zero so the php array will still display them. Quote Link to comment https://forums.phpfreaks.com/topic/189568-group-permissions/#findComment-1007216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.