Welcome Guest, Not a member yet? Register   Sign In
Using Arrays as Field Names
#15

[eluser]bkirkman[/eluser]
To expound on the revision above, the following is the way I have set up the controller and the view. Set up validation in the controller as follows.

Controller:
Code:
...

$this->form_validation->set_rules('item_id[]', 'Item Id', 'trim');
$this->form_validation->set_rules('item_no[]', 'Item No.', 'trim');
$this->form_validation->set_rules('item_description[]', 'Item Description', 'trim');
$this->form_validation->set_rules('item_type[]', 'Item Type', 'trim');

...
item_id is the auto-incremented key value for items in a database and is hidden in the view. The other three fields are columns in the table and will be fields in the view.

Then in the view, I chose to dynamically set the field values depending on the existing condition. I suppose there's a more elegant way to refactor this code, but I, too, spent a lot of time pulling my hair out. I was just happy with a solution.

View:
Code:
...

<table id="tblItems">
    <tr>
    <td class="txtBase">Item No.</td>
    <td class="txtBase">Item Description</td>
    <td class="txtBase">Item Type</td>
    </tr>

&lt;?php if(isset($_POST['item_no'])) : ?&gt;  &lt;!-- this is to set values for forms that do not pass validation --&gt;
&lt;?php foreach($_POST['item_no'] as $row) : ?&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php endforeach; ?&gt;
&lt;?php elseif(isset($item_no)) : ?&gt;  &lt;!-- this is to set values for editing forms that have existing item data --&gt;
&lt;?php $i = 0; ?&gt;
&lt;?php foreach($item_no as $row) : ?&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]', $item_id[$i]); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]', $item_no[$i]); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]', $item_description[$i]); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]', $item_type[$i]); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php $i++; ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php else : ?&gt;  &lt;!-- this is to create new form fields if editing/adding a form with no items --&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php endif; ?&gt;

</table>

...

I'm not sure exactly how others are implementing this "feature." I'm using javascript to add blank fields to the form if the user needs to input more data. That's a discussion for another time. Also, writing the post data back to the database is not exactly trivial when dealing with edits and deletions. Perhaps that will also be a post for another time. Maybe a wiki is in order for forms such as this.


Messages In This Thread
Using Arrays as Field Names - by El Forum - 05-29-2010, 06:50 AM
Using Arrays as Field Names - by El Forum - 05-30-2010, 12:24 PM
Using Arrays as Field Names - by El Forum - 05-30-2010, 03:32 PM
Using Arrays as Field Names - by El Forum - 05-30-2010, 09:35 PM
Using Arrays as Field Names - by El Forum - 05-31-2010, 01:21 AM
Using Arrays as Field Names - by El Forum - 06-01-2010, 07:24 AM
Using Arrays as Field Names - by El Forum - 06-19-2010, 04:12 AM
Using Arrays as Field Names - by El Forum - 06-19-2010, 08:23 AM
Using Arrays as Field Names - by El Forum - 06-19-2010, 12:18 PM
Using Arrays as Field Names - by El Forum - 06-20-2010, 02:01 PM
Using Arrays as Field Names - by El Forum - 06-20-2010, 03:35 PM
Using Arrays as Field Names - by El Forum - 08-02-2010, 03:35 PM
Using Arrays as Field Names - by El Forum - 09-10-2010, 10:43 PM
Using Arrays as Field Names - by El Forum - 09-13-2010, 06:58 AM
Using Arrays as Field Names - by El Forum - 09-13-2010, 03:08 PM
Using Arrays as Field Names - by El Forum - 10-13-2010, 06:33 AM
Using Arrays as Field Names - by El Forum - 11-25-2010, 04:51 AM
Using Arrays as Field Names - by El Forum - 11-25-2010, 05:14 AM
Using Arrays as Field Names - by El Forum - 11-25-2010, 05:15 AM
Using Arrays as Field Names - by El Forum - 04-28-2011, 06:44 AM
Using Arrays as Field Names - by El Forum - 08-03-2011, 09:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB