Good day,
I would like to ask how to handle this on the fly add fields on a form. Basically I have a career form that can add extra fields. My question is
- how can I use an array here to handle the data and display as well if exist?
- how can I capture the data using snippets and save the multiple object data in the custom table?
I use the classextender extra by bobray and barrow the updateprofile snippets.
Heres my sample code for create, update, delete an object
how can i do multiple inserts here using loop and array?
<?php
/*Add in our package*/
$base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/careerservices/';
$cs = $modx->addPackage('careerservices', $base_path.'model/');
$data = null;
$user = null;
$fields = array();
$submit = isset($_POST['login-updprof-btn']) && ($_POST['login-updprof-btn'] == 'Save');
if (isset($modx->user) && ($modx->user instanceof modUser)) {
/*get user id to User Object*/
$user =& $modx->user;
$userId = $user->get('id');
/*Getting the existing data*/
$data = $modx->getObject('csEducation', array('user_id' => $userId), false);
/*Check if the $data*/
if($data) {
$fields = $data->toArray();
} else {
/*Create data if no data exist upon submit*/
$data = $modx->newObject('csEducation');
if($data) {
$data->set('user_id', $userId);
$fields = $data->toArray();
}
}
}
/*Convert any nulls to ''*/
if(!is_array($fields) || empty($fields)) {
return '';
}
if(!empty($fields)) {
/*If the user_id, is no data yet*/
foreach($fields as $key => $value) {
if(empty($value) && ($value !== '0')) {
$fields [$key] = '';
}
}
/*Place the data of user_id if exist*/
$modx->setPlaceholders($fields);
}
/*Sanitize and persistent the data*/
if ($submit) {
$modx->request->sanitizeRequest();
$dirty = false;
foreach($fields as $key => $value) {
if(isset($_POST[$key])) {
if($value !== $_POST[$key]) {
$data->set($key, $_POST[$key]);
$dirty = true;
}
}
}
if($dirty) {
$data->save();
}
}
return '';
Little help is appreciated, thanks!