Jump to content

Array Question (Issue)...

Go to solution Solved by mac_gyver,

Recommended Posts

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){
		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!
Link to comment
Share on other sites

  • Solution

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']);

  • Like 1
Link to comment
Share on other sites

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 by dtyson2000
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.