好吧,也许我犯了一个错误,但我不知道在哪里。你们中的任何人都可以帮助我吗?这是我的代码。
控制器:site.php
public function plus()
{
$modul = $this -> contact_model -> get_names_group();
foreach($modul->result() as $row => $content)
{
$combo[$content->gid] = $content->gname;
}
$data['content'] = $combo;
$data['contacts'] = $this->contact_model->get($this -> session -> userdata('uid'));
$this -> load -> view('plus_member',$data);
}
那就是打开视图,然后我将它发送到数据库。仍在控制器中。
public function add_checked()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('cntact[]', 'Private Contact', 'required|xss_clean');
$this->form_validation->set_rules('cbo_name', 'Group Name', 'required');
if ($this->form_validation->run() == FALSE)
{
$contact = "No <strong>Data</strong> added!";
$this -> json_response(FALSE, $contact);
}
else //success
{
$checked_messages = $this->input->post('cntact'); //selected messages
$this->contact_model->add_checked($checked_messages,
$this -> input -> post('cbo_name'),
$this -> session -> userdata('uid'));
//redirect to
redirect("site/group_contacts");
}
}
我的模型:contact_model.php
function add_checked($cntact,$cbo_name, $uid)
{
foreach ($this->input->post('cntact') as $cntact)
{
$this->db->insert('user_group',array('cid'=>$cntact,'wid' => $cbo_name, 'uid' => $uid));
}
return $this->db->affected_rows() > 0;
}
和我的观点:plus_member.php
<div class="content">
<select name="cbo_name">
<?php foreach($content as $gid =>$gname)
{
echo "<option value=$gid>$gname</option>";
}
?>
</select>
<?php $attribute = array('class' => 'check', 'id' => 'addmember'); ?>
<?php echo form_open('site/add_checked', $attribute); ?>
<button type="submit" class="btn btn-success">
<i class="icon-plus icon-white"></i> Add</button>
<br>
<br>
<table class="table table-striped table-bordered tablesorter" id="tcontacts">
<thead>
<tr>
<td><input type="checkbox" id="select_all" name="select_all"/></td>
<th><i class="icon-tags"></i> ID </th>
<th><i class="icon-user"></i> Name</th>
<th><i class="icon-envelope"></i> Email</th>
<th><i class="icon-headphones"></i> Phone</th>
</tr>
</thead>
<tbody>
<?php if(isset($contacts)) : foreach($contacts as $row) : ?>
<tr>
<td><input type="checkbox" name="cntact[]" class="check" value="<?php echo $row->cid; ?>" /> </td>
<td><? echo $row->cid; ?></td>
<td><? echo $row->name; ?></td>
<td><? echo $row->email; ?></td>
<td><? echo $row->phone; ?></td>
</tr>
<? endforeach; ?>
<? else : ?>
<h2>No records were returned </h2>
<?endif; ?>
</tbody>
</table>
我尝试插入具有相同 gid(组 id)的多个数据。你知道错在哪里吗?因为当我运行它时,我只是得到下拉列表的值为“0”。谢谢你的帮助。