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
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
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
Share on other sites

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
Share on other sites

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