acidking Posted October 11, 2012 Share Posted October 11, 2012 (edited) 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 October 11, 2012 by acidking Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/ Share on other sites More sharing options...
requinix Posted October 11, 2012 Share Posted October 11, 2012 I'd like to insert values of checkboxes as a string using a comma as a delimiter. No. No no no. If you want multiple values then use multiple rows in the table. bad_table id | field ---+------ 1 | a,b,c good_table id | field ---+------ 1 | a 2 | b 3 | c Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384658 Share on other sites More sharing options...
acidking Posted October 11, 2012 Author Share Posted October 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384659 Share on other sites More sharing options...
Jessica Posted October 11, 2012 Share Posted October 11, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384664 Share on other sites More sharing options...
acidking Posted October 11, 2012 Author Share Posted October 11, 2012 (edited) 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 October 11, 2012 by acidking Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384666 Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384676 Share on other sites More sharing options...
Zane Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384681 Share on other sites More sharing options...
acidking Posted October 12, 2012 Author Share Posted October 12, 2012 this is it, this is the bull$hit .. people trying to influence your decisions rather than just give you a straight answer and let you on your way, but oh no way, they must hold onto you. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384689 Share on other sites More sharing options...
MDCode Posted October 12, 2012 Share Posted October 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384690 Share on other sites More sharing options...
Zane Posted October 12, 2012 Share Posted October 12, 2012 (edited) Look, it is literally this simple $query="INSERT INTO boxesTable (boxes) VALUES ('" . implode("','", $_POST['box']) . "')"; No foreach needed. Edited October 12, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384691 Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 Dude, take the language down a notch. If you don't like the answers you get, leave. Don't be a whiny child. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384692 Share on other sites More sharing options...
acidking Posted October 12, 2012 Author Share Posted October 12, 2012 (edited) 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 October 12, 2012 by acidking Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384731 Share on other sites More sharing options...
ManiacDan Posted October 12, 2012 Share Posted October 12, 2012 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 notYou'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. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384749 Share on other sites More sharing options...
acidking Posted October 12, 2012 Author Share Posted October 12, 2012 (edited) 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 October 12, 2012 by ManiacDan Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384754 Share on other sites More sharing options...
ManiacDan Posted October 12, 2012 Share Posted October 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384755 Share on other sites More sharing options...
acidking Posted October 12, 2012 Author Share Posted October 12, 2012 (edited) Sad, very sad. I didn't assume 'expert' around here were going to google something up for me, but thanks now I know who I am dealing with. Edited October 12, 2012 by acidking Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384756 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2012 Share Posted October 12, 2012 (edited) And conversely, we now know exactly who we're dealing with as well. Edited October 12, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384761 Share on other sites More sharing options...
stevew Posted October 12, 2012 Share Posted October 12, 2012 (edited) There is something to be said about understanding why the wrong way is wrong and simply knowing the right way. Edited October 12, 2012 by stevew Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384767 Share on other sites More sharing options...
ManiacDan Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by ManiacDan Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384787 Share on other sites More sharing options...
jcbones Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384792 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2012 Share Posted October 12, 2012 Sarcasm? What sarcasm? Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384804 Share on other sites More sharing options...
ManiacDan Posted October 12, 2012 Share Posted October 12, 2012 I was sarcastic for one sentence in my essay there. Unless YOU'RE being sarcastic...right now. It's not as easy as jcbones makes it seems. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384809 Share on other sites More sharing options...
acidking Posted October 12, 2012 Author Share Posted October 12, 2012 ok seriously, I am unsubscribing from this. Thanks zane. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384810 Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 NEEDS MORE RAGE. Seriously. Is there a site somewhere you can just read ragequits? I haven't seen a good one in a while. These last few are whineyquits. Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384811 Share on other sites More sharing options...
ManiacDan Posted October 12, 2012 Share Posted October 12, 2012 Quote Link to comment https://forums.phpfreaks.com/topic/269372-insert-checkbox-values-into-table-as-delimited-string/#findComment-1384823 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.