Jump to content

Two Checkbox Save Like Array Into One Column In Database


balkan7

Recommended Posts

Hello guys

I'm tried to save data from 2 checkbox into one column in database and retrieve.

 

In this case i have this:

<?
$test = array(1 => "A", 2 => "B");
foreach ($test as $k => $v) {
echo "<input type=\"checkbox\" name=\"c\" value=\"$k\"> $v <br>";
}
?>

If those 2 checkbox selected i want save in base like array 1,2

After this check from database if column data is:

1 = show pic 1

2 = show pic 2

1,2 (array) = show pic 3

Change to

 

<?php
$test = array(1 => "A", 2 => "B");
foreach ($test as $k => $v) {
echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\"> $v <br>";    // change name to post as array
}
?>

 

Then store the total (1,2 or 3) in the DB

 

$total = array_sum($_POST['c'])

Hello Barand

How can add for each checkbox (checked='checked')

$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM table WHERE id=$id");
$data = mysql_fetch_array($sql);
$checkbox = data['c'];

$test = array(1 => "A", 2 => "B");
foreach ($test as $k => $v) {
if ($checkbox == $k) { $checked = " checked='checked'";
echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\"$checked> $v <br>";
}

How can i retrieve if column:

c == 1 only checked A

c == 2 only checked B

c == 3 checked A and B ?

Like this

 

$test = array(1 => "A", 2 => "B");
foreach ($test as $k => $v) {
   $checked = $checkbox & $k ? " checked='checked'" : "";
   echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\" $checked /> $v <br>";
}

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.