dtyson2000 Posted July 8, 2016 Share Posted July 8, 2016 I have the following in a form that edits values already put into a dbase. $alev = array(1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => 'AP'); $dblev = explode(',',$row['hw_lev']); foreach ($alev as $key => $lev){ if(in_array($lev,$dblev)){ echo "<input type='checkbox' name='ud_lev[]' value='$lev' checked> $lev"; } else { echo "<input type='checkbox' name='ud_lev[]' value='$lev'> $lev"; } When I attempt to edit the dbase values, the following occurs: When I select the "A" checkbox, it is input into the database and reflects in the results. When I attempt to edit this, the "A" box is checked and all is well. When I select anything else AND "A" ("1", "A"), it is input into the database and reflects in the results. When I attempt to edit this, the "1" box is checked but the "A" box is NOT checked. This is what I need. I feel like the problem is with the array? But it could be the loop.. Probably something totally staring me in the face. You know how it goes. I was wondering if you see a problem in the array or in the loop. Any thoughts would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/ Share on other sites More sharing options...
Solution mac_gyver Posted July 8, 2016 Solution Share Posted July 8, 2016 you are probably storing some white-space in with the data, which you should be storing not as a comma separated list, but as a separate row in the table for each check box that's checked. what exactly does the $row['hw_lev'] look like using var_dump($row['hw_lev']); 1 Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534337 Share on other sites More sharing options...
dtyson2000 Posted July 8, 2016 Author Share Posted July 8, 2016 (edited) Yep. That was it. Thank you! I found the white-space and everything works as needed. I am interested in your comment on doing it as a separate row in the table for each checkbox. Is this a speed concern, just bad form, or something else? Edited July 8, 2016 by dtyson2000 Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534339 Share on other sites More sharing options...
mac_gyver Posted July 8, 2016 Share Posted July 8, 2016 Assuming the $alev array and the php code/html markup you have posted is accurate, the problem is in the code that's producing the comma separated value you are storing into the database table, which as already stated, you should not be doing anyway. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534340 Share on other sites More sharing options...
dtyson2000 Posted July 9, 2016 Author Share Posted July 9, 2016 Advice taken. I switched it to columns. Makes more sense and a whole lot easier! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534372 Share on other sites More sharing options...
Jacques1 Posted July 9, 2016 Share Posted July 9, 2016 I hope “columns” actually means rows. If you've added a column for each element of the list, that's just as bad as stuffing comma-separated values into a VARCHAR attribute. Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534373 Share on other sites More sharing options...
dtyson2000 Posted July 10, 2016 Author Share Posted July 10, 2016 I hope “columns” actually means rows. If you've added a column for each element of the list, that's just as bad as stuffing comma-separated values into a VARCHAR attribute. Well. Yes. I meant columns. Each column represents, say, an item. Each person can select one item or a combination of items but only one of each. Those items will not change. Is it bad form to make them columns in this way? ID |NAMELAST |NAMEFIRST |1 |2 |3 |4 |5 |A 1 |DOE |JOHN |1 | |1 |1 | | 2 |DOE |JANE | |1 |1 | | |1 3 |DOE |SALLY |1 |1 | | |1 | I know it's not php help so if it's more appropriate to have the discussion elsewhere, I'm good with that. Thanks for your insight! Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534374 Share on other sites More sharing options...
Jacques1 Posted July 10, 2016 Share Posted July 10, 2016 (edited) It is bad. First off, “1”, ... “5” aren't even valid identifiers. You can make them work with the backtick hack, but that's just cumbersome, error-prone and confusing, especially when somebody else might have to work with the application. Secondly, storing list elements as individual columns is inefficient in every aspect, it's an administration nightmare, and it forces you to constantly come up with hacks for even the most trivial tasks. For example, how do you count the number of selected items? You can't just use COUNT(*). You either have to manually go through all columns to calculate the sum of the values, or, which is even worse, you have to mess with the MySQL meta tables. And if you do need a new column in the future, you have to rewrite all of that code everywhere in your application. What if there were 100 items? Would you create 100 boolean columns and carry them around for the lifetime of the application? SQL isn't Excel. Your layout may make perfect sense for a spreadsheet, but it makes no sense in a relational database system. In SQL, data is stored as rows. That's really the entire premise. If you need to store list items (regardless of the number), you create a table with one row per item. As a rule of thumb: When you start numbering your columns, there's a design problem. Edited July 10, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301448-array-question-issue/#findComment-1534375 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.