[eluser]Kyle Johnson[/eluser]
Good Afternoon Everyone,
I am creating a form that will allow multiple line items to be added. Essentially, a web-based packing slip / order form.
I've run into many challenge along the way getting it to the point where the user can enter information, have it show up as a new line in a table.. BEFORE it is entered into the database for storage.
Currently I am storing most of the data within the database version of $this->session and then when the form is submitted for final processing I am using the session information as part of the information to enter into the DB. (I haven't started on the second part of putting it into the DB because I don't like how it's working yet).
I also have it setup in such a way that only a single item can be entered per page load. There is only one "item_id" dropdown, but when they hit the "Add Item" button, it is added to the session variable, and then the form is populated again.
What I am wondering is if you can think of a better way to do this. The way I am going about it seems to be causing a lot of extra coding that doesn't feel very natural with the flow of the rest of the code.
Any help or guidance would be appreciated.
Here is some of the code used. The first part is really messy...
Code:
if($this->input->post('add_item') && $this->input->post('company_master_model_id'))
{
if(!$this->session->userdata('contents'))
{
$session = array(
'contents' => array(
array(
'company_master_model_id' => $this->input->post('company_master_model_id'),
'quantity' => $this->input->post('quantity'),
'serial' => strtoupper($this->input->post('serial')),
'asset' => strtoupper($this->input->post('asset')),
'note' => $this->input->post('note')
)
));
} else {
$oldsession = $this->session->userdata('contents');
$newsession = array(
'company_master_model_id' => $this->input->post('company_master_model_id'),
'quantity' => $this->input->post('quantity'),
'serial' => strtoupper($this->input->post('serial')),
'asset' => strtoupper($this->input->post('asset')),
'note' => $this->input->post('note')
);
array_push($oldsession, $newsession);
$session = array(
'contents' => $oldsession
);
}
$this->session->set_userdata($session);
} else {
if ($this->input->post('clear_item'))
{
$this->session->unset_userdata('contents');
}
}
Loop through the session and add to table of package contents. I use my own form library, but you can kinda interpolate what it does.
Company_master_model_id is a dropdown;
quantity, serial, asset, and note are all text fields;
add_item and clear_item are submit buttons.
Code:
$form->add_anything("<fieldset><legend>Package Contents</legend>");
$form->add_anything('<div id="add_content">');
$form->gen_input("company_master_model_id");
$form->add_anything("<br />");
$form->gen_input("quantity");
$form->add_anything("<br />");
$form->gen_input("serial");
$form->add_anything("<br />");
$form->gen_input("asset");
$form->add_anything("<br />");
$form->gen_input("note");
$form->add_anything("<br />");
$form->gen_input("add_item",'');
$form->gen_input("clear_item",'');
$form->add_anything("<hr />");
$form->add_anything('</div>');
$form->add_anything('<table width="100%"><tr><th style="width: 30px;">Line</th><th style="width: 250px;">Model</th><th style="width: 30px;">Qty</th><th>Serial</th><th>Asset</th><th>Note</th></tr>');
if($this->session->userdata('contents'))
{
$line = 1;
foreach($this->session->userdata('contents') as $val)
{
$form->add_anything('<tr>');
$form->add_anything('<td>'.$line.'</td>');
$i = 0;
foreach($val as $v1)
{
if($i == 0)
{
$form->add_anything('<td>'.$this->company_master_model->model_name($v1).'</td>'); // Get model name instead of showing just the ID
} else {
$form->add_anything('<td>'.$v1.'</td>');
}
$i++;
}
$form->add_anything('</tr>');
$line++;
}
}
$form->add_anything("</table>");
$form->add_anything("</fieldset>");