PHP/HTML 代码
<form method="post" action="index.php">
<table>
<thead>
<th>Name</th>
<th>Age</th>
</thead>
<?php
$speacker = array('joselito','ireneo');
foreach($speacker as $row){
?>
<?php
$radio = array('question 1','question 2');
$name = 'name';
$no = 0;
foreach($radio as $row){
?>
<tr>
<td><input name="<?= $name.$no++; ?>[]" type="radio" value="0" /></td>
<td><input name="<?= $name.$no++; ?>[]" type="radio" value="1" /></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
HTML 预期结果
<form method="post" action="index.php">
<table>
<thead>
<th>Name</th>
<th>Age</th>
</thead>
<tr>
<td><input name="name1[]" type="radio" value="0" /></td>
<td><input name="name1[]" type="radio" value="1" /></td>
</tr>
<tr>
<td><input name="name1[]" type="radio" value="0" /></td>
<td><input name="name1[]" type="radio" value="1" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
这是我的模型
public function sf_insert(){
$name1 = $this->input->post('name1 ');
for ($i = 0; $i < count($name1); $i++) {
$data = array( 'name1' => $name[$i] );
return $this->db->insert('table', $data);
}
}
it save the data but only the first data on the loop.
不确定您的最终目标是什么,但您可以通过稍微修改
input
名称来提交所有数据,如下所示:
<form method="post" action="">
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
$speacker = array('joselito', 'ireneo');
$no = 0;
foreach ($speacker as $row) {
$radio = array('question 1', 'question 2');
$name = 'name';
foreach ($radio as $row) { ?>
<tr>
<td>
<input name="<?= $name ?>[<?= $no++ ?>]" type="radio" value="0"/>
</td>
<td>
<input name="<?= $name ?>[<?= $no++ ?>]" type="radio" value="1"/>
</td>
</tr>
<?php }
} ?>
</tbody>
</table>
<input type="submit" name="submit" value="Submit"/>
</form>
我已将
$no++
移至 []
内,以便您可以获得 $_POST
中的每个值。最后,在您的模型代码中:
public function sf_insert() {
$names = $this->input->post('name');
for ($i = 0; $i < count($names); $i++) {
$data = array('name1' => $names[$i]);
$this->db->insert('table', $data);
}
return true;
}
请注意,使用正确的语法和编码标准始终是一个好方法。在这种情况下,正如@RiggsFolly提到的,
<thead>
需要<tr>
并且<table>
需要<tbody>
。使用缩进还有助于提高代码的可理解性。