Jump to content

Insert Checkbox Values Into Table As Delimited String


acidking

Recommended Posts

Hi all, this my first post.

I'd like to insert values of checkboxes as a string using a comma as a delimiter.

 

So I have these checkboxes:

<input type="checkbox" class="checkbox" name="box[]" value="1">One</input>
<input type="checkbox" class="checkbox" name="box[]" value="2">Two</input>
<input type="checkbox" class="checkbox" name="box[]" value="3">Three</input>

 

 

Then I'd have a code like this, which is obviously wrong hence I wrote it for illustration only, it should also have an explode function for the delimiter:

if (isset($_POST['box'])) {
$newbox = array();
foreach($_POST['box'] as $boxArr){
array_push($newbox, $boxArr);
}

$query="INSERT INTO boxesTable (boxes) VALUES ('$newbox')";

}

 

Any help would be much appreciated.

Edited by acidking
Link to comment
Share on other sites

I perfectly understand that, and I find myself having to explain this to everyone, but the purpose and use of this particular table doesn't require multiple columns for the checkboxes. In any case, please do ignore what the ideal way is and let me know if you have an answer for the above.

Link to comment
Share on other sites

No, because then when you have trouble actually using the data, you'll be asking for help again and you'll get the same response. There's a reason everyone keeps telling you to do it that way.

 

However, imploding an array into a delimited string is so simple that if you insist on doing it, you should have been able to find an example by now...

Link to comment
Share on other sites

The data is not meant to be used. And what is it to you how do I want to use the data? Would you like to be my project manager? If it's really that simple I would've got an answer by now instead of bull$hit.

Edited by acidking
Link to comment
Share on other sites

Why would you store data that won't be used again?

If you don't know how to use google, that's your problem. Too bad your project manager can't see this and all the other places where apparently you've been told its a bad idea. If you don't have the balls to tell them, we can.

Link to comment
Share on other sites

However, imploding an array into a delimited string is so simple...

 

http://php.net/implode

implode is what you are looking for, but like you've been advised on plenty of times so far, it is not the correct way to go and you will ultimately end up with deeper problems if you persist with the implode method.

 

But like I have already learned from trial and error. You cannot teach trial and error, so go crazy and figure out the reason behind our advise yourself. Then you can come back and tell us, "You were right"

Edited by Zane
Link to comment
Share on other sites

This forum is not for giving you answers, it is to help you to find your errors on your own so you may learn to debug your own code in the future. If you want straight forward answers, there is a freelancer section.

I don't need a freelancer, I am trying to learn, the books don't exactly cover each possibility you can do with php.

 

Dude, take the language down a notch. If you don't like the answers you get, leave. Don't be a whiny child.

How about; if you don't have the answers, f*** off.

Look, it is literally this simple

$query="INSERT INTO boxesTable (boxes) VALUES ('" . implode("','", $_POST['box']) . "')";

 

No foreach needed.

Thank you kindly! Though I am not sure I'll be asking question here again because clearly no one has the answers, and the community proved unreliable and unsupportive.

 

I am not working on a serious project, I have a small project I am experimenting with in order to learn, so it doesn't matter whether it's the ideal way or not, I just need to know how such things are done, but why do I have to explain this on length? Makes no sense.

Edited by acidking
Link to comment
Share on other sites

I am not working on a serious project, I have a small project I am experimenting with in order to learn, so it doesn't matter whether it's the ideal way or not
You're learning wrong. You are learning the wrong way. Once you've "learned," the way you do things will be wrong, and nobody will hire you.

 

I'm not sure why that's "bull$hit" advice. You are doing it wrong. You're learning the wrong way. Why would you learn to do it wrong if you're learning? Start out doing it right.

 

We're happy to give you answers. In fact, you asked "how do I implode a list" and instead of getting the wrong answer, you got expert level advice on system design. If all you want is a robot to tell you how to implode a list, use google, not a forum. You asked a group of experts for their opinion, and you got it.

Link to comment
Share on other sites

Where did I ask for their 'opinion'? Are you hallucinating? or perhaps you're in the wrong post? In the post we're in now I asked how to specifically implode values of checkboxes. Yes I used google, I didn't particularly find how to implode values of checkboxes.

 

Eventually my final code turned out to look slightly different:

 

if (isset($_POST['box'])) {
$checkbox = mysql_real_escape_string(implode(", ", $_POST['box']));
}

 

This is proof to you that I am learning and all I needed was pointers, so you can go and *#$% yourself you're full of *&*%#.

 

My first post was polite, it didn't work, my second post was polite, nothing came out of it, seems there's a method for getting around on here.

Edited by ManiacDan
Link to comment
Share on other sites

You are doing it wrong. Sorry if that upsets you. Doing it right is way easier. You'll see what we mean in just a little while.

 

And when you post on a forum, you're asking members to comment. You're demanding that their comments be direct answers to your questions, but there's nothing anywhere which requires us to say "yes sir right away sir" and go google something for you. You're asking for our comments, which are opinions or advice. Our advice is universally "do it another way, this way is wrong."

 

Good luck with that design. Not too much longer now before you have your "oh" moment.

Link to comment
Share on other sites

There is something to be said about understanding why the wrong way is wrong and simply knowing the right way.

He'll figure out the "why" soon enough on his own, but since you appear to be reasonable and interested I'll explain:

 

By storing a delimited list in a single field, you remove your ability to search, sort, update, and delete. That's basically the entire point of databases now isn't it?

 

If my data looks like this:

1,5,234,88,124,2,79

 

How do I search for anything matched to number 88? The only real way to do that is:

 

WHERE FIND_IN_SET(88, theField);

 

You could also do:

WHERE theField LIKE '88,%' OR theField LIKE '%,88,%' OR theField LIKE '%,88'

 

Both of those queries are very very inefficient, because they require parsing huge string lists.

 

Now how do you return the item with the largest number of IDs associated with it? You...can't really. I guess you could try to count the number of commas:

SELECT name, (LENGTH(theField) - LENGTH(REPLACE(theField, ',', ''))) / LENGTH(',') FROM theTable GROUP BY theField ORDER BY (LENGTH(theField) - LENGTH(REPLACE(theField, ',', ''))) / LENGTH(',') DESC

 

What about editing? How do I remove ID 88 from a specific record? Well that's easy! I just...select it out of the database...put it into a PHP variable...explode it...loop through it...unset the proper index...implode it...and reinsert it into the database.

 

If the original advice of a lookup table was used INSTEAD, what we have is:

Find things linked to 88:

SELECT theTable.* FROM theTable JOIN lookupTable USING (id) WHERE lookupTable.lookup = 88;

 

Find all things ordered by number of lookups:

SELECT theTable.name, COUNT(lookupTable.lookup) FROM theTable JOIN lookupTable USING (id) GROUP BY theTable.name ORDER BY COUNT(lookupTable.lookup) DESC;

 

Delete 88 from record 5:

DELETE FROM lookupTable WHERE id = 5 AND lookup = 88;

 

Once the tables are done correctly, every query is very basic datbase 101. You don't need an entirely separate programming language, you don't need to wrap your head about the stupid voodoo magic I did with the LENGTH() function, you don't need to handle edge cases involved in lists of numbers, your stuff is designed right so it works right

 

And that, is why we said he was doing it wrong. He was.

Edited by ManiacDan
Link to comment
Share on other sites

I'm glad that people told(tell) me when I was(am) doing things wrong. Coding issues seem non-existant now.

 

The best things I have learned since being told "you're doing it wrong":

Database Normalization,

Relational Database Design,

Logical Programming,

and...

 

wait for it...

 

forum sarcasm!!!

Edited by jcbones
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.