在Codeigniter中管理动态表单并更新数据库?
I have a database with a number of tables, dbtable
.
I'm creating a form with php and I layout the form using <table>
.
Each <tr>
in the table corresponds to one row in a dbtable
.
So, the more rows in a dbtable
, the more tr-tags.. you get it.
My question is:
How should I name the input-tags to be able to use the POST-variables to update the database accordingly?
At the moment, I'm naming them 1_name, 2_name, 3_name
for one dbtable data
then 1_1_valueA, 1_1_valueB, 1_2_valueA, 1_2_valueB
for data that belongs to 1_name, 2_name etc.
But I feel this is not such a good approach..
我有一个包含多个表的数据库, 我正在用php创建一个表单,我使用 每个 因此, 我的问题是: p>
我应该如何命名输入标签才能使用 相应更新数据库的POST变量? p>
目前,我正在为一个dbtable数据命名 但我觉得这不是一个好方法.. p> \ n div> dbtable code>。 p> \ n
&lt; table&gt; code>布局表单。 p>
&lt; tr&gt; ; code>表中的 code>对应于
dbtable code>中的一行。 p>
dbtable code>中的行越多, 更多tr-tags ..你得到它。 p>
1_name,2_name,3_name code>
then
1_1_valueA,1_1_valueB,1_2_valueA,1_2_valueB code>。 p>
name them like this:
item[0][db_row_id]
item[0][value_a]
item[0][value_b]
item[1][db_row_id]
item[1][value_a]
item[1][value_b]
where the numbers 0 and 1 etc are just assigned as indicies in a POST array
you will be able to retrieve all of the items with $_POST['item']
or in CI $this->input->post('item')
and it will look like this:
array(
array('db_row_id'=>val,'value_a'=>val,'value_b'=>val),
array('db_row_id'=>val,'value_a'=>val,'value_b'=>val)
)
<input name="reords[0]['Name']" />
<input name="reords[0]['Age']" />
<input name="reords[0]['City']" />
<input name="reords[1]['Name']" />
<input name="reords[1]['Age']" />
<input name="reords[1]['City']" />
you can access data in sever side by
$this->input->post('reords');
it is an array like this
array(array('Name'=>'post val','Age'=>'post val','City'=>'post val'),
array('Name'=>'post val','Age'=>'post val','City'=>'post val')
)
you can use codegniter upadte_batch
to add these records to db
http://ellislab.com/codeigniter/user-guide/database/active_record.html