chaseman Posted May 4, 2011 Share Posted May 4, 2011 Let's say I have a list like this and in the parenthesis are the values: Page A (24) Page B (63) x Page C (33) Page D (22)x Page F (87)x Page E (45) The X mark the ones which are checked with a checkbox, and the values get inserted into the database in ONE row as follows: 63 22 87 (separated with spaces) And now an update to the list has been made, one x has been unchecked, the list looks as follows: Page A (24) Page B (63) x Page C (33) Page D (22) Page F (87)x Page E (45) How can I now know which checkbox exactly has been unchecked, so that I can remove that specific value from the row in the database? Once I've figured that out I'll figure out the rest. Basically what I need to know is: - that an uncheck has been done (removing) - an which value exactly has been unchecked Just FYI I need this for the admin panel of WordPress ones the user has checked a few options then the options will be saved and they'll be auto checked on the next visit. Checkbox list has the name like this: name="exclude[]" And then I simply use a foreach loop to go through all the values. Here's the code for the list: <ul> <!-- Right Section --> <?php $pages = get_pages(); $get_exclude_id = explode (' ', $get_exclude); foreach ($pages as $pagg) { ?> <li> <?php $option = '<input type="checkbox"'; if (in_array ($pagg->ID, $get_exclude_id)) { $option .= " checked "; } $option .= 'name="exclude[]" value="'. $pagg->ID .'" />'; $option .= $pagg->post_title; ?> </li> <?php echo $option; } ?> </ul> Here's the code for updating the ROW in the database. // add or update navi bar options if ($submit) { $get_exclude = get_option ('exclude_pages'); $page_id_string = $get_exclude . " "; foreach ($page_id as $value) { $page_id_string .= $value . " "; } update_option ('exclude_pages', $page_id_string); } Only works for adding values, but not for removing as of now. I hope I could explain clear enough to follow and I hope to hear some suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/235555-how-to-know-when-a-checkbox-has-been-ticked-off/ Share on other sites More sharing options...
Psycho Posted May 4, 2011 Share Posted May 4, 2011 Why would you be saving a list of values in a single field in the database? You should use an associated table with an individual record for each value with a foreign key reference to the parent record. But, let's say you go with the flawed approach above. Why do you need to know which values were unchecked? When the user submits the form, just replace the value with the ones that are still checked. Rough example $excludeVals = implode(' ', $_POST['exclude']); $query = "UPDATE [table_name] SET [field_name] = $excludeVals WHERE user_id = $userID"; Quote Link to comment https://forums.phpfreaks.com/topic/235555-how-to-know-when-a-checkbox-has-been-ticked-off/#findComment-1210634 Share on other sites More sharing options...
chaseman Posted May 4, 2011 Author Share Posted May 4, 2011 As I've mentioned this is for WordPress, and it may seem absurd to you but this is quite common practice in WordPress development, you can not simply create and manage databases like you'd normally do. You have one "options" database, and there you're allowed to add your own rows with values, and if you're an economical and efficient thinker who also considers that many other people use the same database like plugin developers for example, then you'd group all your associated values in one row instead of cluttering the whole database up and making it a mess for every body else. Hope that clears it up. I will try your suggestion and see if it will do the trick, the code got a bit complex so that I lost the overview. =P Thank you a lot BTW, your approach worked very great, just FYI I wrote above code when I started and knew nothing about WordPess' admin panel, that's why it's so flawed - all I had to do with rewrite the code into this very simple and clean version: if ($hide_pages_save) { foreach ($page_id as $id) { $page_id_string .= $id . " "; } update_option ('exclude_pages', $page_id_string); } Now it's working as expected. The WordPress specific update_option function does the same as your SET command of your MySQL query. Quote Link to comment https://forums.phpfreaks.com/topic/235555-how-to-know-when-a-checkbox-has-been-ticked-off/#findComment-1210638 Share on other sites More sharing options...
Psycho Posted May 4, 2011 Share Posted May 4, 2011 So, a normalized database is a cluttered database? I guess being able to run efficient queries and do proper joins are just fluff. I don't use WordPress (I guess I know why now), but I can't believe that it doesn't allow you to properly use a database. foreach ($page_id as $id) { $page_id_string .= $id . " "; } No need to run a foreach loop, use the functions that PHP makes available to you. $page_id_string = implode(' ', $page_id); Quote Link to comment https://forums.phpfreaks.com/topic/235555-how-to-know-when-a-checkbox-has-been-ticked-off/#findComment-1210646 Share on other sites More sharing options...
chaseman Posted May 5, 2011 Author Share Posted May 5, 2011 lol mjdamato, I was surprised at first too, but then I got used to it, I think it is part of the robustness of WordPress, since ever I'm using it I have not seen it break once, only third party plugins will cause problems, and now imagine all these plugin developers have full control over the database -> even more problems. I'm not a pro though, but that's how I imagine it to say the least. And thanks for the tip about ditching the foreach loop, didn't even know it will work like that! Quote Link to comment https://forums.phpfreaks.com/topic/235555-how-to-know-when-a-checkbox-has-been-ticked-off/#findComment-1210650 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.