Reputation: 749
I have $_POST
values like this.
The below value is coming from a form.
What I would like to do is.
Author Code 1 and Author Code 3 has a new flag called [chkName_1] => 1
So this value alone should get inserted into the database
Author Code 2 does not have the flag and that should be ignored.
[author_code_1] => 1
[author_name_1] => Author 1
[chkName_1] => 1
[author_code_2] => 2
[author_name_2] => Author 2
[author_code_3] => 3
[author_name_3] => Author 3
[chkName_3] => 1
The above array should run in a loop and the insert statement should look something like this.
insert Into author_log (`author_code`,`author_name`) values (1,'Author 1');
insert Into author_log (`author_code`,`author_name`) values (3,'Author 3');
In other words, check the flag values and if it is set to 1 then insert into db.
Thanks,
Kimz
PS: I don't even have any idea of handling this and running a forloop.
Upvotes: 0
Views: 49
Reputation: 771
<?php
$authors = array(
'author_code_1' => 1,
'author_name_1' => 'author1',
'chkName_1' => 1,
'author_code_2' => 2,
'author_name_2' => 'author2',
'author_code_3' => 3,
'author_name_3' => 'author3',
'chkName_3' => 1
);
// _ CODE STARTS HERE
$list = array();
foreach ($authors as $key => $entry) {
if (strpos($key, 'author_name_') !== false) {
$index = str_replace('author_name_', '', $key);
$list[$index]['name'] = $entry;
} elseif (strpos($key, 'author_code_') !== false) {
$index = str_replace('author_code_', '', $key);
$list[$index]['code'] = $entry;
} else if (strpos($key, 'chkName_') !== false) {
$index = str_replace('chkName_', '', $key);
$list[$index]['chk'] = $entry;
}
}
// ^ CODE ENDS HERE
print_r($list);
?>
Use this code to transform that mess of a post request in something more operation-friendly. Though you should really modify your form.
Upvotes: 1