Forum Moderators: coopster

Message Too Old, No Replies

Checkboxes in one field

Dynamic Checkboxes

         

Saint Honore

5:11 am on Sep 21, 2006 (gmt 0)

10+ Year Member



Hello,

I have a situtaion where i should be able to store 24 checkboxes value in table.
Can i store those values in one single field and retive in later?

Any help would be apprrecitated.

Warm Regards

tomda

6:03 am on Sep 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep! It's possible.

If your checkbox has the same name attribute, then add [] to the name

<input type="checkbox" name="test[]" value="1">
<input type="checkbox" name="test[]" value="2">
<input type="checkbox" name="test[]" value="3">

If all checkbox are checked, it will return an array as follow array("1","2","3").

In PHP, you should:

1. Check that POST variable exist, if not it's an empty array

if(isset($_POST["test"])) {$test = $_POST["test"];} else {$test=array();}

2. Then explode the array and check every single element using a loop (safer since you want to insert it in a database). Also, you can unset all empty element.

for ($i="0"; $i<count($test); $i++) {
if(!is_numeric($test[$i])) {$test[$i]="";}
if(empty($test[$i])) {unset($test[$i]);}}

3. Then store it in the database - turn you array into a string. I like to store array in my database with "<>" as a separator (easier to explode/implode). There are many other different ways to store your data in your database, you can use whatever you want but note that empty space is not the best option if values have space (e.g. "test 1" instead of "1")

$test = implode ("<>", $test);
$test = "<>".$test."";
$sql = "INSERT INTO test_table (`id`, `test_value`) VALUES (NULL, '$test')";
$res=mysql_query($sql) or die ("Fail to add test_value");

The code above will store <>1<>2<>3 (a nice string)

4. Retrieve stored data using explode - this will turn your string into an array again.

$sql = "select id, test_value from test_table limit 1";
$res=mysql_query($sql) or die ("Fail to get test");
$test = explode('<>',$pres[1]);
for ($i="0"; $i<count($test); $i++) {
if(empty($test[$i])) {unset($test[$i]);} }

As you can see, I usually checked again that there are no empty string. This is not really necessary, but I prefer double-check than getting errors.

5. Form - Automatically check boxes which has been checked - Just use in_array to see if current test[] value has been checked.

echo '<input type="checkbox" name="test[]" value="1";
if(in_array("1", $test)) {echo ' checked';}
echo '><input type="checkbox" name="test[]" value="2";
if(in_array("2", $test)) {echo ' checked';}
echo '><input type="checkbox" name="test[]" value="3";
if(in_array("3", $test)) {echo ' checked';}
echo '>';

That's it!
Tomda

Saint Honore

3:51 am on Sep 22, 2006 (gmt 0)

10+ Year Member



Hello Tomda,

It was really a good tutorial for me, iam really thankful for you. I never thought of having people like you in the world who can sacrifice their time for someone like me. It is really a superb response and perfect timings. I really thankful for you.

Best wishes

Saint Honore

5:58 am on Sep 22, 2006 (gmt 0)

10+ Year Member



Hello,
THis is my form.

<form name="services" action="addservices.php" method=post>
<table border=1 cellspacing=1 cellpadding=1 width=70% bordercolor='#0000ff' align="center">
<tr>
<td>
<input type="checkbox" name="services[]" value="1">Illumination Tour
<input type="checkbox" name="services[]" value="2">Provins, a Medieval City
</td>
<td>
<input type="checkbox" name="services[]" value="3">Wine tasting
<input type="checkbox" name="services[]" value="4">Private Walking Tour of Paris
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="services[]" value="5">Chateaux de Versailles
<input type="checkbox" name="services[]" value="6"> Chantilly
</td>
<td>
<input type="checkbox" name="services[]" value="2">Bateaux Mouches
<input type="checkbox" name="services[]" value="8">Giverny & Monet's Garden
</td>
</tr>
</table>

And here is my form which is for retriving values

<?

if(isset($_POST["services"]))
{
$services = $_POST["services"];
}
else
{
$services=array();
}
$qrySel = "INSERT INTO test (bookingid, services) values('$bookingid', '$services')";
echo $qrySel;
$result = mysql_query($qrySel);
?>
</form>

The problem is get the values of checkboxes as array. I mean it is jsut priting array.

Any help will be appreciated

Warm Regards

RonPK

11:32 am on Sep 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Saint, you seem to be overlooking Tomda's step 2 and 3. That's where the array is converted into a string that can be stored in the database.

Also, you may want to have a look at the PHP methods serialize [php.net] and unserialize [php.net], which make array-to-string conversions and back easier.

Saint Honore

10:51 am on Sep 28, 2006 (gmt 0)

10+ Year Member



Hello Frineds,

Thanks again, Now i have different situation similar to this, i have a field in DB called bookingstatus whose value will be <>1<>2<>3
Now after certain action I want to add one more extra value i.e. <>1<>2<>3<>4 how can i update this, should i have to check the previous values? how do i do update?

Warm Regards

jatar_k

5:21 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



pull the previous value and append your new value to the end of the string, then re insert it