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>
values for tr 1 is: Translate & Searches & Email
values for tr 2 is: Phone & Chat & Business
values for tr 3 is: jimi
I 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:
<table>
tr 1:
<tr>
<input name="name" value="Translate">
<input name="explanation" value="Searches">
<input name="tv" value="Email">
<input type="checkbox" name="checkbox[]" 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: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 likeIn 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.
And the loop becomes