Jump to content

Checkbox array, imploding and exploding help.


TeddyKiller

Recommended Posts

I haven't actually tackled this.

 

I have checkboxes, all with different names. Although might be useful to put them into

checkbox[] for the name. So that it's arrayed.

 

What I want to do, for each checkbox CHECKED to put it in an array and it'll end up like this as a result.

value1, value2, value3, value4

Depending on the value of the checkbox. This could use an implode, but I think wouldn't it put all the checkboxes together, and maybe some won't have a value, so it could end up like this..

, , , , , , , value 1, value 2 etc

Also if the names were "checkbox[]" wouldn't this make it $_POST['checkbox[]']

I dont understand how that's arrayed? I'm sorry this is tricky for me.

 

Basically.. For each checkbox submitted, implode it with a comma.

 

Then, I'm assumin this is correct. We get the value from the database, we then do something like this.

$split = explode(', ', $row['value']);

Although.. What I need to do, is because each checkbox is different values, I want the ones that are in the database, (value wise) to be checked on the form.

 

Usually it'd be simple like..

if($row['value'] == 'car') { echo 'Car checkbox here'; }

Though because it's an array, and we don't know whats checked.. how would I do it?

 

This is a huge issue for me.

Please help.

Cheers

Yes, that would make it a multi-dimensional array. It would be accessed and written to the same as any other array, except you need to include the additional index: $_POST['checkbox']['id1'], $_POST['checkbox']['id2'], etc. When you submit the form, print_r() the $_POST array to get a better idea of its structure.

 

You'll want to store the values each in their own field, probably in a separate table, using a foreign key relation, not as a long string in a field for the main record.

I would have all the checkboxes with the same name liek you said checkbox[]

 

The on the $_POST i have something like this

$quals = implode(",",$_POST['quals']);

 

The use a foreach on the $split

 

foreach($split as $s) {
     if($s == 'car') { echo 'Car checkbox here'; }
}

I'm sorry, I don't quite understand what you are trying to say?

AdRock, thank you. I just thought that I do have a method that unsets empty values. The implode should actually work.

Is $_POST['quals'] Is this checkbox name.. name="quals[]" ? I'm slightly confused.

 

Your foreach can't be done like that, all checkboxes should be displayed, but the ones with the value in the database, should be checked. This is the problem, your script wouldn't do that. It'll only display the checked ones.

 

I have put this together for a form validation

$fields = array();
$_POST = clean($_POST,0,0);
$fields[] = (isset($_POST['proname']) && !empty($_POST['proname'])) ? "`pro_name` = '" . $_POST['proname'] . "'" : '';
$fields[] = (isset($_POST['protag']) && !empty($_POST['protag'])) ? "`pro_name` = '" . $_POST['protag'] . "'" : '';

foreach ($fields as $key => $field) {
if (empty($field)) {
	unset($fields[$key]);
        }
}

$checkbox = implode(', ', $_POST['checkbox']);
$check1 = "`interested_in` = '". strtolower($checkbox) . "'";

 

The string of $check1 should be

`interested_in` = 'value1, value2, value3, value4'

etc etc. Though would this include the empty values, The foreach above.. removes empty values, but only for $fields[]. I'm not sure it's possible to put the checkboxes into a $fields array..

Rather confused :(

Archived

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

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