Jump to content

Checkbox help...part 2


emvy03

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.