我试图将值放入MySQL表中,如下所示:
| ----------物理---------- | ---- Level 1 ----- |
| ----------物理---------- | ---- Level 2 ----- |
| ----------物理---------- | ---- Level 3 ----- |
| ----------微积分--------- | ----第1级----- |
| ----------微积分--------- | ---- Level 2 ----- |
(我不知道怎么做桌子,但我想你明白了)
我要说我要把数学"数学"进入sbjct_name列和" Level 1"," Level 2"," Level 3"值进入级别列。
基本想法来自this pict (selecting value for a same input text value)。
而不是插入"数学"每个级别多次, 我试图用复选框标记一次提交它。它将类似于this pict (with checkbox)。
我不知道如何实现这一目标(或者如果CI可能的话)。
这是我的控制器中的一些代码,
public function add_subject()
{
$data = array();
if ($this->input->post('savebttn'))
{
$this->form_validation->set_rules('sbjct_name', 'Subject Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('level', 'Level', '|required|is_natural');
$this->form_validation->set_error_delimiters('<span class="fielderror">','</span>');
if ($this->form_validation->run() == FALSE)
{
$data['reset'] = FALSE;
}
else
{
//I don't know what to do here
//I don't know what to do here
//I don't know what to do here
//I don't know what to do here
}
}
$data['level'] = $this->admin_model->get_checkbox_option('level', 'lvl_id', 'lvl_name');
$data['page'] = 'createsubject';
$this->load->view('admin/main', $data);
}
这是模型中的复选框视图功能
public function get_checkbox_option($table, $id, $name, $selected=0)
{
$query = $this->db->get($table);
$select= '';
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$selected_option = ($selected == $row[$id]) ? 'selected = "selected" ':' ';
$select .='<input type="checkbox" name="level" value="'.$row[$id].'" '.$selected_option.'>'.$row[$name].'<br>';
}
}
return $select;
}
这是视图上的表单,
<form action="" method="post" id="createcategoryform">
<table>
<tbody>
<tr>
<td><div class="label">Subject Name</div></td>
<td><div class="input">
<input type="text" name="sbjct_name" size="39" class="ui-corner-all input-text validate[required]">
<?=form_error('sbjct_name');?>
</div></td>
</tr>
<tr>
<td><div class="label">Level</div></td>
<td><div class="input-text ui-corner-all validate[required]">
<?=(isset($level)) ? $level: '';?>
</div><?=form_error('level');?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" name="savebttn" class="input-button ui-corner-all ui-state-default"></td>
</tr>
</tbody>
</table>
</form>
如果你能给出任何线索,我将不胜感激。 :d
答案 0 :(得分:1)
<强>第一强>
将您的复选框设为数组,使其看起来像这样
$select .='<input type="checkbox" name="level[]" value="'.$row[$id].'" '.$selected_option.'>'.$row[$name].'<br>';
注意名字。你的name="level"
和我的name="level[]"
,那么你可以将所有级别作为一个变量获取
<强>第二强>
让你的控制器像这样
if ($this->form_validation->run() == FALSE)
{
$data['reset'] = FALSE;
}
else
{
// make array container for input batch
$insertData = array();
if(!empty($this->input->post('level'))) {
foreach($this->input->post('level') as $level) {
$tempArray = array(
'sbjct_name' => $this->input->post('sbjct_name'),
'level' => $level
);
array_push($insertData, $tempArray);
}
// it's better to put this in model
// but for example purpose I put it there
$this->db->insert_batch('table', $insertData);
//do what you want to do here
}
}
希望你明白@nasamikhail