I am writing a little php page to help generate forms from a csv file.
The csv file can have fields like “type, name, default” etc etc etc. The output form is compatible with CI’s Form validation library, and the layout is based on blueprintcss.org ‘s Form layout.
IMHO, it’s easier to generate a form view, put it in views folder and load it later, rather than write the form directly within controller.
The code is simple:
function index() {
$errors = array();
$userfile['upload_path'] = '/tmp/';
$userfile['allowed_types'] = 'csv';
$userfile['overwrite'] = TRUE;
$userfile['remove_spaces'] = TRUE;
$this->upload->initialize($userfile);
if ($this->upload->do_upload('userfile')) {
$fileinfo['userfile'] = $this->upload->data();
} else {
$errors['userfile'] = $this->upload->display_errors();
}
if (count($errors) == 0) {
echo "<h1>Result source code</h1>\n";
$handle = fopen($fileinfo['userfile']['full_path'], 'r');
$fields = array('label','info','type','name','value');
$out = "";
while (($data = fgetcsv($handle, 4096)) != FALSE) {
$tmp = array_combine($fields, $data);
extract($tmp);
$out .= "<p><label for='$name'>".(($info)?"<abbr title='$info'>$label</abbr>":$label)."</label><br/>\n";
$out .= "<input id='$name' type='$type' name='$name' value='<?= set_value('$name', \$$name); ?>' /></p>\n";
$out .= "\n";
}
fclose($handle);
echo "<pre>\n".htmlentities($out)."</pre>";
} else {
echo "<h2>CodeIgniter Form Generator</h2>\n";
echo "<form action='".current_url()."' method='post' enctype='multipart/form-data'>\n";
echo "<input type='file' name='userfile'/>\n";
echo "<input type='submit' value='Generate Form' />\n";
echo "</form>";
}
}
Right after the “extract($tmp)” line, you could handle the output based on type column, default values, etc etc etc… With this, I could use OpenOffice Calc to export csv and then upload it to localhost ci to generate the form I need in no time.
Cheers