Jump to content

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


dtyson2000

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!
Link to comment
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.
Link to comment
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]
Link to comment
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]
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.