Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

multiple checkboxes

0 views
Skip to first unread message

Gaga

unread,
Dec 3, 2006, 7:10:30 AM12/3/06
to
1.) what is the best way to store multiple checkboxes into table. (e.g. if
you 20 checkboxes there is no reason to make 20 table fields ? )

2.) How to pull this stored checkbox data so i can display which checkboxes
are checked and which not ?

Koncept

unread,
Dec 3, 2006, 11:57:01 AM12/3/06
to
In article <ekueq5$3qb$1...@ss408.t-com.hr>, Gaga <rg2...@hotmail.com>
wrote:

> 1.) what is the best way to store multiple checkboxes into table. (e.g. if
> you 20 checkboxes there is no reason to make 20 table fields ? )

I would serialize an array and store the serialized string in the DB.

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" name="cb[1]" value="on" checked="checked" />
<input type="checkbox" name="cb[2]" value="on" checked="checked" />
<input type="checkbox" name="cb[3]" value="on" />
<input type="checkbox" name="cb[4]" value="on" checked="checked" />
<input type="checkbox" name="cb[5]" value="on" />
<input type="submit" value="submit &#x2192;"/>
</form>

<?php
/*
POST data looks like this...
Array
(
[cb] => Array
(
[1] => on
[2] => on
[4] => on
)
)
*/

echo serialize( $_POST['cb'] );
// a:3:{i:1;s:2:"on";i:2;s:2:"on";i:4;s:2:"on";}

> 2.) How to pull this stored checkbox data so i can display which checkboxes
> are checked and which not ?

$cbdata = unserialize( $data ); // will give you the checked boxes as
an array.

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche

Usenet

unread,
Dec 3, 2006, 12:53:06 PM12/3/06
to
> 1.) what is the best way to store multiple checkboxes into table.
> (e.g. if
> you 20 checkboxes there is no reason to make 20 table fields ? )

Depends on what you want. There might well want to make twenty fields, or
not.


> 2.) How to pull this stored checkbox data so i can display which checkboxes
> are checked and which not ?

Depends on how/where you're storing the data. You have to pull all the data
so you can check the boxes or not.

I could understand if you thought this not helpful, but really you're not
asking specific enough questions to be able to give what I'd call helpful
answers.

Mark

IchBin

unread,
Dec 3, 2006, 1:10:19 PM12/3/06
to

I am new to php also and ran in to the same question. I have 2 rows and
26 columns for chars A->Z. This code is in a FORM NAME="author_abbrev"
and I POST the action to $_SERVER['PHP_SELF']. You can see it at
http://ichbin.9999mb.com if the free hosting site is running.

print '<table><tbody><tr>';
$loopValue = 1;
//
// Loop thru and build the 26 radio buttons with the correct
selected one
foreach (range('A', 'Z') as $letter)
{
if( $loopValue == 14)
{
print '</tr><tr>';
}
print '<td><input type="radio" name="abbr_letter" value="'.
$letter .'" ';
if( $letter == $_POST['abbr_letter']) print 'checked="checked" ';
print ' onclick="submit()" />'.$letter.'</td>';

$loopValue = $loopValue +1;
}
print '</tr></tbody></table>';


--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Message has been deleted

la...@portcommodore.com

unread,
Dec 4, 2006, 9:47:27 AM12/4/06
to

This is what I had come up with, using arrays when in the script and
varchar in the DB, you get all the related boxes into one field, and
have human readable represntation of the contents.

Here's the linik:

http://www.portcommodore.com/quickpage.php?id=mixed+selection+lists

(this page is a work in progress, sorry for any errors)

Larry

kenoli

unread,
Dec 4, 2006, 3:11:43 PM12/4/06
to
For storing the data, I use a linking table. Thus I have a lookup
table that has the names of my checkboxes in one column, with another
column with unique IDs for each checkbox name. Then I have a linking
table where I have one column for CHECKBOX IDs and the other for PERSON
IDs (assuming you are associating the checkbox names with people).
Thus if a person were identified with 6 different checkboxes, there
would be 6 PERSON ID rows in the linking table associated with each
CHECKBOX ID associated with the appropriate checkbox name.

There are several reasons for doing this:

1. Your person table doesn't get clogged up with a bunch of fields

2. You can change the checkbox names with screwing up existing data.

3. You can easily retrieve the information using joins or other
apporpiate query.

When I display the checkboxes in the form, I do it with the code shown
below. This script gets the checkbox names from the checkbox table and
displays them in an array in a table cell, adding a new cell whenever
it needs to to create a new column. This is set up for 3 columns. You
can change the code for various numbers of columns depending on how
many checkboxes you have.

If you want to show which checkboxes have been checked for which person
IDs, you can derive that data by selecting the checkbox IDs on the
appropriate person ID from the linking table and using that information
to get the names of the checked checkboxes from the checkbox table and
then inserting that between the checkbox tags in the html using
$_POST[].

--Kenoli

//::::::::::::::: Create interest area checkboxes. :::::::::::::::::://

<?php
function interest_checkboxes() {

echo "<fieldset class='callborder'><legend class=\"title\">Interest
Area</legend>\n";

echo "<table class=\"callborder\"><tr>\n<td>";

global $row;

$query = "SELECT * FROM tlu_interest_area ORDER BY interest_area
ASC";
$result = @mysql_query($query);
$number = mysql_num_rows($result);

$i=0;
$second_col = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i > ($number/3) && $second_col == 0) {
echo "</td><td>";
$second_col = 1;
}
elseif ($i > (2*$number/3) && $second_col == 1) {
echo "</td><td>";
$second_col = 2;
}

if (in_array('6', $interest)) {$checked = 'checked';} else {$checked
= '';}

echo "<input class=\"checkbox\" type=checkbox $checked
name=\"interests[]\" value=\"{$row[interest_area_id]}\"
/>{$row[interest_area]}</input><br/>\n";
$i ++;

}

echo "</td></tr></table></fieldset>";

}

?>

la...@portcommodore.com

unread,
Dec 4, 2006, 11:15:49 PM12/4/06
to

kenoli wrote:
> For storing the data, I use a linking table. Thus I have a lookup
> table that has the names of my checkboxes in one column, with another
> column with unique IDs for each checkbox name. Then I have a linking
> table where I have one column for CHECKBOX IDs and the other for PERSON
> IDs (assuming you are associating the checkbox names with people).
> Thus if a person were identified with 6 different checkboxes, there
> would be 6 PERSON ID rows in the linking table associated with each
> CHECKBOX ID associated with the appropriate checkbox name.
>

I see where you are going with it, that's prety slick.

One of the benefits of the arrays concept is you can group the
checkboxes, but I gureess in the checkbox table you could put a
groupname there too.

kenoli

unread,
Dec 5, 2006, 3:08:05 AM12/5/06
to
Yes. If you knew where you wanted to insert a group name, you could
stop the foreach at each insertion point and add the proper html text
as you want to format it.

ulyx

unread,
Dec 6, 2006, 12:59:27 PM12/6/06
to
I hope i have write this correct. I get no error, but i can see any new
updated data in db. There is no updating ?
I have created tested table and field.

?>
<form action="testCheckBox.php" name="test" method="post">
<?
$weekdays = array( "mon" => "Monday",
"tue" => "Tuesday",
"wed" => "Wednesday",
"thr" => "Thursday",
"fri" => "Friday",
"sat" => "Saturday",
"sun" => "Sunday" );

// initialize $days as being empty:
$days = array();

echo "<b>Days of Service:</b><br />";
foreach( $weekdays as $value => $desc ) { //walk through possible values
echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
already selected, check the box
echo '>'.$desc.'<br />'; //echo the descriptive name of the item
}

$daylist = implode(",",$days);
//$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
record_id = $record_id");
$days = explode(",", $dbTest ['db_days']);
?>
<input type="submit" name="Submit" value="Submit">
</form>


IchBin

unread,
Dec 6, 2006, 1:59:29 PM12/6/06
to
ulyx wrote:
> I hope i have write this correct. I get no error, but i can see any new
> updated data in db. There is no updating ?
> I have created tested table and field.
>
> ?>

don't need this ?>

> <form action="testCheckBox.php" name="test" method="post">
> <?
> $weekdays = array( "mon" => "Monday",
> "tue" => "Tuesday",
> "wed" => "Wednesday",
> "thr" => "Thursday",
> "fri" => "Friday",
> "sat" => "Saturday",
> "sun" => "Sunday" );
>
> // initialize $days as being empty:
> $days = array();
>
> echo "<b>Days of Service:</b><br />";
> foreach( $weekdays as $value => $desc ) { //walk through possible values
> echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
> echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
> already selected, check the box
> echo '>'.$desc.'<br />'; //echo the descriptive name of the item
> }
>
> $daylist = implode(",",$days);
> //$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
> record_id = $record_id");
> $days = explode(",", $dbTest ['db_days']);
> ?>
> <input type="submit" name="Submit" value="Submit">
> </form>
>
>

ulyx

unread,
Dec 7, 2006, 4:20:24 AM12/7/06
to
don't need this ?>
This is just typing error, but there is still no update :-((

----------------------------------------------------------


<form action="testCheckBox.php" name="test" method="post">
<?
$weekdays = array( "mon" => "Monday",
"tue" => "Tuesday",
"wed" => "Wednesday",
"thr" => "Thursday",
"fri" => "Friday",
"sat" => "Saturday",
"sun" => "Sunday" );

// initialize $days as being empty:
$days = array();

echo "<b>Days of Service:</b><br />";
foreach( $weekdays as $value => $desc ) { //walk through possible values
echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
already selected, check the box
echo '>'.$desc.'<br />'; //echo the descriptive name of the item
}

$daylist = implode(",",$days);
//$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
record_id = $record_id");
$days = explode(",", $dbTest ['db_days']);
?>
<input type="submit" name="Submit" value="Submit">
</form>

------------------------------------------------------------------------


la...@portcommodore.com

unread,
Dec 11, 2006, 4:11:57 PM12/11/06
to
note: you need "s around days[] in your INPUT entry (name="days[]")

The code you posted will return to the form action target an array:
days (if any days are checked) in $_POST. This is a code snippet not
the entire form logic, You still have to validate the POST data,
implode and use a DB INSERT/UPDATE statement to finish it all.

Here is some more code for how to bring the form data in:

//this will get your days back to a local array (when you read in your
POSTed from data, if there is no data it set up an empty array (in case
you go back to the entry form)

If isset($_POST['days']) {
$days = $_POST['days'];
} else {
$days = array();
}
// Add validation code here...

//If the data is valid, before you put it in SQL you will need to make
your array into a string (again you should use addslashes escape any
potentially bad data.)

$sqldays = implode(",",$days);

//(you would add the result string into your update/insert query for
the days field.)

//then when reading in back in from SQL (for editing):

$days = explode(",",$sqlresult['daysfield']);

As for complete form logic,
Here is a write up of how to code a one file form/db handling script
(not suited for everything but is good for single record entry/edit
work.)
http://www.portcommodore.com/quickpage.php?id=Single+File+Edit+Script

(I havent posted the example script, should do that soon.)

Larry

0 new messages