problem with inserting multiple string POST in a database table by json_encode()
I use jQuery to add a new input. Each row in table contains several inputs, and I want to add the data to the database as an array:
<table>
tr 1: <tr>
<input name="name" value="Translate">
<input name="explanation" value="Searches">
<input name="tv" value="Email">
</tr>
<br>
tr 2: <tr>
<input name="name" value="Phone">
<input name="explanation" value="Chat">
<input name="tv" value="Business">
</tr>
tr 3: <tr>
<input name="user" value="jimi">
</tr>
</table>
DEMO
values for tr 1 is: Translate & Searches & Email
values for tr 2 is: Phone & Chat & Business values for tr 3 is: jimiI use json_encode()
on tr 1
and tr 2
to encode data and insert it together into the database. tr 3
is not in that array, it is alone value:
$data = array(
'units' => json_encode(
array(
'name' => $this -> input -> post('name'),
'explanation' => $this -> input -> post('explanation'),
'tv' => $this -> input -> post('tv')
)
),
'user' => $this -> input -> post('user')
);
$this -> db -> insert('hotel_submits', $data);
The problem here is that only values "tr 2" inserted into the database.
I want value both(tr 1 and tr 2) together to be insert in the database.I'm having in database:
{"name":Phone,"explanation":Chat,"tv":Business}
and I want this
{"name":Translate,"explanation":Searches,"tv":Email, "name":Phone,"explanation":Chat,"tv":Business}
UPDATE:
If there is several checkbox
in each tr (1 & 2)
and some of them are checked
, How are inserted(they) in the database as below:
DEMO
<table>
tr 1:
<tr>
<input name="name" value="Translate">
<input name="explanation" value="Searches">
<input name="tv" value="Email">
<input type="checkbox" name="c开发者_运维百科heckbox[]" value="Mootools" checked>
<input type="checkbox" name="checkbox[]" value="PowerTools" checked>
<input type="checkbox" name="checkbox[]" value="Read">
</tr>
tr 2:
<tr>
<input name="name" value="Phone">
<input name="explanation" value="Chat">
<input name="tv" value="Business">
<input type="checkbox" name="checkbox[]" value="Clientcide">
<input type="checkbox" name="checkbox[]" value="Library" checked>
<input type="checkbox" name="checkbox[]" value="Framework" checked>
</tr>
tr 3:
<tr>
<input name="user" value="jimi">
</tr>
</table>
I want in database:
[{"name":"Translate","explanation":"Searches","tv":"Email",
"checkbox":["Mootools","PowerTools"]
},{"name":"Phone","explanation":"Chat","tv":"Business","checkbox":["Library","Framework"}
]
Since inputs in your rows share their names, the inputs in the last row rewrite values of other inputs. To overcome this, you need to assign names with []
and iterate over posted data:
<table>
tr 1: <tr>
<input name="name[]" value="Translate">
<input name="explanation[]" value="Searches">
<input name="tv[]" value="Email">
</tr>
<br>
tr 2: <tr>
<input name="name[]" value="Phone">
<input name="explanation[]" value="Chat">
<input name="tv[]" value="Business">
</tr>
tr 3: <tr>
<input name="user" value="jimi">
</tr>
</table>
$units = array();
$names = $this->input->post('name');
$explanations= $this->input->post('explanation');
$tvs= $this->input->post('tv');
//this works ONLY if you have equal number of names, explanations and tvs
foreach ($names as $idx=>$name){
$units[] = array(
'name'=>$name,
'explanation'=>$explanations[$idx],
'tv'=>$tvs[$idx]
);
}
$data = array('units'=>json_encode($units), 'user'=>$this -> input -> post('user'));
$this -> db -> insert('hotel_submits', $data);
This code is not ideal, but it should work, so it's a good starting point. Also, you can't have what you want with json_encode
, so it will look like
[{name:..., explanation:..., tv:...},{name:..., explanation:..., tv:...}]
In fact you can, but than you'll have hard time generating and parsing it.
Update well, that's tough. The idea is to assign indexes to your inputs explicitly, i.e.
tr1:name[0], explanation[0], tv[0], checkbox[0][],checkbox[0][],checkbox[0][]
tr2:name[1], explanation[1], tv[1], checkbox[1][],checkbox[1][],checkbox[1][]
And the loop becomes
$checkboxes= $this->input->post('checkboxes');
foreach ($names as $idx=>$name){
$units[] = array(
'name'=>$name,
'explanation'=>$explanations[$idx],
'tv'=>$tvs[$idx],
'checkboxes'=>$checkboxes[$idx]
);
}
精彩评论