emvy03 Posted July 27, 2011 Share Posted July 27, 2011 Hi, A few of you may remember but I recently solved a problem I was having in uploading check box data to MySQL database, which I managed to solve. At the time all I wanted to do was take check box data and put it into a table, and done so using an array which took the selected check boxes and put it onto separate rows. I.e. If i selected 4 check boxes, then 4 rows would be created. What I now want to do is create a separate column in my table for each check box and have a 0 or 1 inserted into there based on whether the box was ticked. My code at the moment is: <?php include "storescripts/connect_to_mysql.php"; if(isset($_POST['size'])) { $size = $_POST['size']; $n = count($size); $i = 0; while ($i < $n) { mysql_query("INSERT INTO events (value) VALUES ('$language[$i]')") or die(mysql_error()); $i++; } echo "</ol>"; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> Select the size you would like<br> <input name="size[]" type="checkbox" value="Small"> Small<br> <input name="size[]" type="checkbox" value="Medium"> Medium<br> <input name="size[]" type="checkbox" value="Large"> Large<br> <input name="send" type="submit" id="send" value="Upload"> </form> Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/ Share on other sites More sharing options...
fenway Posted July 27, 2011 Share Posted July 27, 2011 And? Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248024 Share on other sites More sharing options...
emvy03 Posted July 28, 2011 Author Share Posted July 28, 2011 Hi. For instance, I want a column in my database table for small, medium and large. Then, based on whether a user ticks the check box for the respective size a 1 or 0 would be inserted into the table. I was thinking I would need an if statement that says; if the box is ticked then insert a 1 into the table, else put a 0? Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248323 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 That's pretty much exactly what you would need. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248407 Share on other sites More sharing options...
emvy03 Posted July 28, 2011 Author Share Posted July 28, 2011 Hello. Ta for the reply, the only thing is I have the idea of what needs doing but not the know how of how to execute it. So I need a little help. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248409 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 I see, well, as far as I can tell there are two ways of doing it. Because you are storing your checkbox info in an array, you can either implode the array, then run preg_match()'s against it, or loop through it with a for each loop. I think the for each loop is probably the easiest option. $checkList = array (); $checklist = $_POST['size']; $smallOut = 0; $mediumOut = 0; $largeOut = 0; foreach ($checklist as $option){ if ($option = 'Small'){ $smallOut = 1; } else if ($option = 'Medium'){ $mediumOut = 1; } else if ($option = 'Large'){ $largeOut = 1; } } echo 'Checking values:<br>'; echo '<table><tr><th>Small</th><th>Medium</th><th>Large</th></tr>'; echo "<tr><td>$smallOut</td><td>$mediumOut</td><td>$largeOut</td></tr></table>"; Something like that should do, it's messy and untested and probably needs some work but it should get you on the right track. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248421 Share on other sites More sharing options...
emvy03 Posted July 28, 2011 Author Share Posted July 28, 2011 Hi, Thanks so much for the help. I've done that, and added an if(isset()) function in there to solve a foreach error. But, all that happens when I tick a box is a 1 is generated for the small size, regardless of which box I tick. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248576 Share on other sites More sharing options...
Muddy_Funster Posted July 29, 2011 Share Posted July 29, 2011 could try and change to this, but I'm not sure if it's right or not $checklist = $_POST['size[]']; Just a thought Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1248932 Share on other sites More sharing options...
emvy03 Posted July 30, 2011 Author Share Posted July 30, 2011 Hi mate. I tried that suggestion but it doesn't seemed to have solved it. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1249310 Share on other sites More sharing options...
Muddy_Funster Posted July 30, 2011 Share Posted July 30, 2011 add these two lines of code just before the if() print_r($checklist); exit(); and then tick all three checkboxes and post what you get back as the contents of $checklist Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1249399 Share on other sites More sharing options...
fenway Posted July 31, 2011 Share Posted July 31, 2011 This is rapidly becoming php-only issue... Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1249779 Share on other sites More sharing options...
emvy03 Posted August 3, 2011 Author Share Posted August 3, 2011 Hi, Sorry, I'm on holiday at the moment so my ability to access the internet is somewhat temperamental. I applied your suggestion and the following is displayed: Array ( [0] => Small [1] => Medium [2] => Large ) Thanks mate!! Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1251141 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 ok, tweeked and tested the foreach (I made a totaly stupid mistake with the = ) here's the sample code I used for testing <form method="post" action=""> Select the size you would like<br> <input name="size[]" type="checkbox" value="Small"> Small<br> <input name="size[]" type="checkbox" value="Medium"> Medium<br> <input name="size[]" type="checkbox" value="Large"> Large<br> <input name="send" type="submit" id="send" value="Upload"> </form> <br><br><br><br> <?php if (@$_POST['send']){ $checklist = array(); $checklist = $_POST['size']; foreach ($checklist as $option){ if ($option == 'Small'){ $smallOut = 1; } if ($option == 'Medium'){ $mediumOut = 1; } if ($option == 'Large'){ $largeOut = 1; } } echo 'Checking values:<br>'; echo '<table><tr><th>Small</th><th>Medium</th><th>Large</th></tr>'; echo @"<tr><td>$smallOut</td><td>$mediumOut</td><td>$largeOut</td></tr></table>"; } ?> That works now, standalone. I also took the "<?php echo $_SERVER['PHP_SELF'];?>" out of your form, you don't need it in there, and it's better if it's not. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1251146 Share on other sites More sharing options...
emvy03 Posted August 3, 2011 Author Share Posted August 3, 2011 Yes!! Thank you so much for all your help. I guess now instead of echo-ing values, I put a simple mysql query there to insert the values? Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1251151 Share on other sites More sharing options...
emvy03 Posted August 3, 2011 Author Share Posted August 3, 2011 Thank you. Everything is now solved!! Muddy_Funster, you sir, are a complete legend!! Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1251154 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 lol, steady on, I'm actualy a pretty poor coder compared to most of the regulars on here. Just glad I could help. Good luck. Link to comment https://forums.phpfreaks.com/topic/242944-checkbox-helppart-2/#findComment-1251158 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.