Hi, I'm creating an administration area for a little web-app and I've come across a problem that I've never encountered before.

The form that I'm working on is used for the maintenance of a table that has several properties. One of these properties can be selected with checkboxes, and then recorded in the correct column in a comma-delimited list. The problem that I'm having is constructing/alterring the list - for some reason I can't for the life of me figure out how to do it.

Oh yeah, the fun part is that the properties that are in the checkbox are created on the fly from another table, which is why i didn't decide to create a column for each option and flag it.

Here's the snippet of code that creates the checkbox area:

<?php
    $sql2 = "SELECT * FROM tbl_action";
    $result = mysql_query($sql2);
			
    while($act_row = mysql_fetch_array($result)) {
	echo "<input type=checkbox value=\"".$act_row['action_id']."\"> ";
	if(strstr($myrow['spy_action'], $act_row['action_id']))
   	    echo "checked>";
    echo ucwords($act_row['action_name'])."<br>";
    }
?>

Ok, 2 tables - spy and action. All columns from spy are called spy_name, and from action are act_name.

So far I've considered using Javascript, making the code create a new column every time a new action is created, and various other techniques that have been thrown out as soon as I think of them.

This is the last part of the admin area, and once it's complete I'll be a very happy boy.

I don't think I understand you correctly. All you want to do is check multiple checkboxes, put those checked values in a comma delimited string, and insert it in some table?

Hi inscissor, thanks for the reply. Let me see if I can clarify myself.

I want to be able to check/uncheck multiple checkboxes, and put those values in a comma delimited string, and then stick it in a column, that's right.

The part that's throwing me for a loop though is that the checkboxes are created from the database (the options come from another table) in the first place.

I'm starting to think I'm overthinking this whole problem, but still haven't figured out how to do it.

Well, your problem seems to stem from how you're creating the checkboxes in the loop. First of all, here:

echo "<input type=checkbox value=\"".$act_row['action_id']."\"> ";

you don't create a name for the checkbox, so how are you going to retrieve it? The way it's usually done is you give the checkbox a name with open and closed bracks, like this: values[]

So when you pass this over, it'll pass it over as an array. So this is all you have to do:

<?php 
	$sql2 = "SELECT * FROM tbl_action"; 
	$result = mysql_query($sql2); 
			 
	while($act_row = mysql_fetch_array($result)) { 
	echo "<input type=checkbox name="values[]" value=\"".$act_row['action_id']."\"> "; 
	if(strstr($myrow['spy_action'], $act_row['action_id'])) 
		   echo "checked>"; 
	echo ucwords($act_row['action_name'])."<br>"; 
	} 
?>

So when you submit this, it'll submit an array of the values of only the ones that got checked. So you loop through this array, in this case being values[]. Also, when you're retrieving this form variable, retrieve it as $_POST and not $_POST']. I'm sure you can do the rest. Anything else, let me know.

Thanks Inscissor,

To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.

Thanks Inscissor,

To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.

Were you able to figure it out? I do this all the time, so it's no sweat. I'll give you code if you want.

Given that the data is stored in an array, what type of structure should I use for storing it?

Given that the data is stored in an array, what type of structure should I use for storing it?

Didn't you say you wanted it in a comma delimited string? So your structure would be a string. All you would need to do is loop through the array, and as you're looping, append\concatenate the adjacent strings in the array. Fortunately, you could also use the implode() function, so you'll have something like this:

$values = $_POST['value']; // getting value array from form variable
$delimited_values = implode(",", $values);
echo $delimited_values;

It actually doesn't matter if they're stored in a comma-delimited string or not (but yes, i did say that that's how i wanted it).

My only requirement is that the data can be retrieved and written to easily. I've been looking at serializing the array and putting it into the column, but if you have another recommendation...

That sounds fine. Maintaining it as an array gives you a lot of flexivity. You can manipulate it easily with the PHP array functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.