I have an array $contact that looks like this :
Array
(
[0] => Array
(
[type] => Textbox
[label] => Company
[name] => company
[properties] => a:2:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}
[language] => EN
[weight] => 1
[nid] => 5
)
[1] => Array
(
[type] => Textbox
[label] => First name
[name] => first_name
[properties] => a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}
[language] => EN
[weight] => 2
[nid] => 5
)
[2] => Array
(
[type] => Textbox
[label] => Last Name
[name] => last_name
[properties] => a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}
[language] => EN
[weight] => 3
[nid] => 5
)
And I'd like to change it to :
INSERT INTO `form` (`nid`, `language`, `label`, `name`, `type`, `properties`, `options`, `weight`) VALUES
( 5, 'EN', 'Société', 'societe', 'Textbox', 'a:2:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', NULL, 1),
( 5, 'EN', 'First name', 'first_name', 'Textbox', 'a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', NULL, 2),
( 5, 'EN', 'Last Name', 'last_name', 'Textbox', 'a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', NULL, 3);
So I tried this :
function insert_contact($array){
foreach($array as $key => $value){
if ($key == 'fields'){
$columns = array_keys($value);
$values = array_values($value);
}
}
$sql = "INSERT INTO `".'form' . "` (`".implode("`, `", $columns)."`)";
$sql .= " VALUES ('".implode("', '", $values)."') ;";
return $sql;
}
And it worked for the only the first element of the array,but when I do :
foreach ($var as $key => $value) {
var_dump(insert_contact( $var[$key]));
}
It displays me this error : Notice: Undefined variable: columns
Is there a way to avoid this error and generate a clean sql query? Thanks
insert_contact(). Ensure it contains the keyfields.