Jump to content

Archived

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

dtyson2000

[Resolved] Retrieve Checked Boxes from Database (Barand-style)

Recommended Posts

Hi all...

I've read just about every thread here about my question but still cannot come up with a solution. It's probably something completely stupid that I'm missing, so please excuse me, if so.

I have a form on which there are multiple checkboxes that set parameters for the particular database entry (by setting parameters, I mean, a given entry may be type 1,4,6,9 out of 22 choices). The initial entry for these "types" is:

[code]
<input type="checkbox" name="type_array[]" value="Type 1"> Type 1<br>
<input type="checkbox" name="type_array[]" value="Type 2"> Type 2<br> ... etc.
[/code]

Now, when the user clicks "Submit", the types are imploded by the following and inserted as VARCHAR into the database:

[code]
if ($type_array)
{
$type = implode(', ', $_POST['type_array']);
};
[/code]

It inserts perfectly. But maybe not THAT perfectly. When I go to pull it back out to say - edit - an existing item, I just cannot get the checkboxes to reflect those that were checked when it was initially entered into the database.

I've tried various solutions from posts here and am ready to rip my hair out.

Here's where I am now:

I've used code from Barand and all of the checkboxes and type descriptions come up, though none of those that should be checked are checked. Here's what I have:

[code]
$type=mysql_result($result,$i,"type");

//... snip

$new_type_array = array (
        1 => 'Big Book',
        2 => 'Closed (Alcoholics Only)',
        3 => 'Open (Guests Welcome)',
        4 => 'Discussion',
        5 => 'Mini-Lead', //...etc. etc.
);

foreach ($new_type_array as $typeid =>$desc) {
if ($_POST['type']) {
$chk = in_string($typeid, 'type') ? 'checked' : '';
}
else $chk = '';
echo "<input type='checkbox' name='ud_type_array[]' value='$typeid' $chk>$desc<br>";
}
[/code]

I'm sure that I need to explode the "$type" that I imploded before and have tried just replacing "implode" with "explode" but it still isn't doing it.

Anyway... Again, I apologize if it's something really simple that I'm overlooking.

I truly appreciate any help anybody can offer. Thanks!

Share this post


Link to post
Share on other sites
so if i'm correct, which i'm probably not, you're trying to keep a checkbox checked based on the value of a mysql field row.  very doable, in fact it was just something i came across.

the way i did it was very simple, and it didn't involve using implode() at all.

what i did was i had a field called `checked`, which either had a value of 0 for not checked, or 1 for checked. (tinyint)

then, when i retrieved the row data, I did something like this to see whether I wanted to have the checkbox checked or not
[code]
<?php
$conn = mysql_connect($server, $username, $password) or die(mysql_error());
$rs = mysql_select_db($database_name) or die(mysql_error());
$sql = "SELECT * FROM `table` WHERE `id`=\"$selected_id\"";
$rs = mysql_query($sql, $conn) or die(mysql_error());
//fetch array of row info
$row = mysql_fetch_array($rs);
//assuming the column is named "checked"
$msg = "<input type=\"checkbox\" name=\"toggle\" ";
if($row['checked'] == 1){
$msg .= "checked ";
}
$msg .= "/>";
echo $msg;
?>
[/code]

so if the column `checked` is set to 1, it echoes "checked" inside the input tag, but if not, it leaves that part out.

this works for 1 single checkbox, but I'm sure if you improvise you can have different columns for different checkboxes and make it work out.

Share this post


Link to post
Share on other sites
If you are wanting something like "1,4,6,9" to be stored in the table then your checkbox values should be 1,2,3 etc and not "Type 1, Type 2, Type 3" etc.

So let us assume you have extracted $dbtype from the database and it contains "1,3,5"

[code]
<?php
 
  $dbtypes = '1,3,5';
  $db_array = explode(',', $dbtypes);
 
  $new_type_array = array (
        1 => 'Big Book',
        2 => 'Closed (Alcoholics Only)',
        3 => 'Open (Guests Welcome)',
        4 => 'Discussion',
        5 => 'Mini-Lead', //...etc. etc.
);

foreach ($new_type_array as $typeid =>$desc) {

$chk = in_array($typeid, $db_array) ? 'checked' : '';

echo "<input type='checkbox' name='ud_type_array[]' value='$typeid' $chk>$desc<br>";
}
 
?>
[/code]

Share this post


Link to post
Share on other sites
Excellent, Barand!

You hit the nail on the head. I changed the values of my checkboxes and now the script (with some tweaks to follow your post) calls and checks the boxes correctly.

Thanks, again!

Share this post


Link to post
Share on other sites
A better (correct, ie normalized) way to store this type of data is to have a separate table for the types contining the id of the parent record and the type code. This would be linked to the parent record via the id and to a type description table via the type code
[pre]
table            table_type          type
------          ----------          ---------
id    ----+      rec_id        +---- type_code
etc      +----- table_id      |    type_desc
etc              type_code  ---+
etc
[/pre]

Share this post


Link to post
Share on other sites

×

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.