Jump to content

Form Arrays


timmah1

Recommended Posts

I have form with numerous checkboxes labled as so

 

<input type="checkbox" name="medication[]" value="128">

<input type="checkbox" name="medication[]" value="12">

 

When I post the form, how do I get everyone of the boxes checked into the database?

 

I'm lost.

 

Thanks in advance!

Link to comment
Share on other sites

Each medication is associated with a certain value.

 

I need the values to go into the database as 128,12,etc

 

A foreach loop adds every med in a different row, it all needs to be in the same field

 

 

Doing that is bad practice. I fyou have a many to one relationship you should have a child table for those records. But, I will answer your question nonetheless.

 

The great thing about checkboxes is that if they are not checked their value is not posted. So, concatenating the values with commas is very simple:

$meds = implode(',', $_POST['medication']);

Link to comment
Share on other sites

Thank you mjdamato Does exactly what is needed.

 

This is for charting. When someone enters their medications on the form, it puts them into the database field "medications", along with their username and a timestamp.

I'm not sure why it's 'bad practice', it works for what I need it to. Are there great drawbacks for doing this?

Link to comment
Share on other sites

The "correct" way to do it would be to insert a new row for every different medication they selected.

 

So you would make a new table called something like "user_medications", and it would look something like

 

userID | medicineID

-------------------

  1            128

  1            12

  4            68

 

That way it just makes it easier to select the information you need. The way your doing it your going to have to select the filed, split up all the medicine ID's, then do a million queries to get specific information on each medication (which I assume is in another table). If you do it the way I described, you can get all the information you need with one query using a join.

Link to comment
Share on other sites

Are there great drawbacks for doing this?

 

Depends on your definition of "great drawbacks" and your future needs. Having the data "normalized" such as pocobueno1388 explained you will have much greater flexibility. Without knowing your current or future needs I can't say how much of a benefit, if any, it would be.

 

But, let's say you needed a report of how many users are taking each medication. The only way to do it with the current structure would be to query the entire database and then create a post-processing script in PHP to analyze the data. Which would create a lot of unnecessary overhead. With a child table like pocobueno1388 showed you could get the numebr of users taking each medication with the following query:

 

SELECT count(*)
FROM medications
JOIN users on medications.user_id = users.id
GROUP BY medications

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.

Guest
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.