rondog Posted December 13, 2010 Share Posted December 13, 2010 I have a table that shows all the users in my table. Each user is either a user or an admin. I have radio buttons for each user determining if they are a user or an admin Right now I have each RB group named userlevel0, userlevel1, userlevel2 etc.. How can I get all the groups dynamically? I attempted an html array but failed. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 You'll have to be more specific, I don't know what "get all the groups dynamically" means. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 POST them dynamically..I don't know how many rows their are unless I do a select to get the num rows first which I am trying to avoid. If I get the num rows first and its equal to, say 5, then I know their are 5 groups of radio buttons meaning: userlevel0, userlevel1, userlevel2, userlevel3, userlevel4 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Use userlevel[] and then when posted you have a userlevel array so you can foreach over something or use count($_POST['userlevel']). I would need to see the code and exactly what you want to accomplish. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 I tried that..the problem is, each user is their own group. if I do userlevel[], all the rows are one big group. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Need code and explanation. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 I cant be any more clear dude. Each row is its own group in the same form. When I submit I want to POST all the radio button groups, but since I don't know how many rows their are I can just do $_POST['userlevel0'], $_POST['userlevel1']..I need to know how many rows their is. I think I am just going to do the select before I post solution... Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Well, I'm positive I can give you a solution, but not if you can't be more clear. Every row is it's own group does not make sense to anyone but you. Anyone that can help you would want to see code. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 group = radio button group... If you look at the picture in the OP, you can see the rows.. each row you can select admin or user...each row is its own radio button group..when I POST, I don't know how many rows their are. This is my code so far: if (isset($_POST['savebtn'])) { // post the radio button groups } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 I tried that..the problem is, each user is their own group. if I do userlevel[], all the rows are one big group. I don't know what this means. Arrays are the way to go here but it seems that you have done something wrong before and are unwilling to follow the advice and try it again. Post your form code. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 Let me rephrase that quote. I tried using an HTML array. Each user (row) has its own radio button group. If I set the name of the radio button group to userlevel[], its one giant row meaning I can only select 1 radio button across all 4 rows you see in the image above. This is my form code: <form method="post"> <table cellpadding="4" border="0"> <tr style="text-align:center;font-weight:bold;background-color: #777777; color: #ffffff;"> <td>Username</td> <td>Email</td> <td>First Name</td> <td>Last Name</td> <td>Deactivate?</td> <td>User Level</td> </tr> <tr class="usertable"> <td><div align="center">some username</div></td> <td><div align="center">someemail@domain.com</div></td> <td><div align="center">some first name</div></td> <td><div align="center">some last name</div></td> <td><div align="center"><a href="?do=mngu&a=deactivate&id=28">Deactivate</a></div></td> <td><label> <input type="radio" name="userlevel0" value="1" checked="checked"/> Admin</label> <br/> <label> <input type="radio" name="userlevel0" value="0" /> User</label></td> </tr> <tr style="background-color: #4f4f4f" class="usertable"> <td><div align="center">some username</div></td> <td><div align="center">someemail@domain.com</div></td> <td><div align="center">some first name</div></td> <td><div align="center">some last name</div></td> <td><div align="center"><a href="?do=mngu&a=deactivate&id=27">Deactivate</a></div></td> <td><label> <input type="radio" name="userlevel1" value="1" checked="checked"/> Admin</label> <br/> <label> <input type="radio" name="userlevel1" value="0" /> User</label></td> </tr> <tr class="usertable"> <td><div align="center">some username</div></td> <td><div align="center">someemail@domain.com</div></td> <td><div align="center">some first name</div></td> <td><div align="center">some last name</div></td> <td><div align="center"><a href="?do=mngu&a=deactivate&id=26">Deactivate</a></div></td> <td><label> <input type="radio" name="userlevel2" value="1" /> Admin</label> <br/> <label> <input type="radio" name="userlevel2" value="0" checked="checked"/> User</label></td> </tr> <tr style="background-color: #4f4f4f" class="usertable"> <td><div align="center">some username</div></td> <td><div align="center">someemail@domain.com</div></td> <td><div align="center">some first name</div></td> <td><div align="center">some last name</div></td> <td><div align="center"><a href="?do=mngu&a=deactivate&id=25">Deactivate</a></div></td> <td><label> <input type="radio" name="userlevel3" value="1" checked="checked"/> Admin</label> <br/> <label> <input type="radio" name="userlevel3" value="0" /> User</label></td> </tr> </table> <input type="submit" name="savebtn" value="Save" /> </form> Now you can see all the radio button groups...I need to know how many radio button groups to post when I hit save and since the users table constantly changes in size, I need to know how many to post Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Wow, that's was like trying to wring water out of a rock But just based on the code that you've shown, I would use the id as the array index: <td><div align="center"><a href="?do=mngu&a=deactivate&id=28">Deactivate</a></div></td> <td><label> <input type="radio" name="userlevel[28]" value="1" checked="checked"/> Admin</label> <br/> <label> <input type="radio" name="userlevel[28]" value="0" /> User</label></td> Then on the receiving page, if all you want is the count you would do: $count = count($_POST['userlevel']); But depending on what you're doing you would normally just foreach(): foreach($_POST['userlevel'] as $id => $value) { // UPDATE table_name SET user_level = $value WHERE user_id = $id } Or something similar. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 yeah sorry, I'll use the term radio button group rather than group next time to be more clear. Also, I wasn't aware you could define the HTML array index. That is very good to know. With your suggesting, this is what I ended up with and it works. Thanks for stickin through if (isset($_POST['savebtn'])) { foreach ($_POST['userlevel'] as $id => $value) { $query = mysql_query("UPDATE members SET userlevel = '" . $_POST['userlevel'][$id] . "' WHERE id = '" . $id . "'"); } } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Glad to hear it, and you can replace $_POST['userlevel'][$id] with $value (less code). Also, you should really use mysql_real_escape_string() on any variables in your query that are coming from the form. Quote Link to comment Share on other sites More sharing options...
rondog Posted December 13, 2010 Author Share Posted December 13, 2010 Glad to hear it, and you can replace $_POST['userlevel'][$id] with $value (less code). Also, you should really use mysql_real_escape_string() on any variables in your query that are coming from the form. I tried using $value and it seems to be empty. If i echo $value its just blank Quote Link to comment 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.