Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 dtyson2000

dtyson2000
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 28 October 2006 - 05:53 PM

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:

<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.

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

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

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:

$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>";
			}

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!

#2 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 28 October 2006 - 06:11 PM

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
<?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;
?>

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.
fuck it or fight it

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 October 2006 - 06:54 PM

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"

<?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>";
	}
  
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 dtyson2000

dtyson2000
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 28 October 2006 - 09:46 PM

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!

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 October 2006 - 10:00 PM

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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users