无法调用codeigniter中的下拉值

时间:2014-05-12 18:06:52

标签: codeigniter multiple-insert

好吧,也许我弄错了,但我不知道在哪里。你们任何人都可以帮助我吗? 这是我的代码。

controller: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 &nbsp;</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”。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

可以尝试围绕值

的单引号
  echo "<option value='$gid'>$gname</option>";

答案 1 :(得分:0)

use this
<?php foreach($content as $gid =>$gname)
                {?>
                    <option value="<?php echo $gid; ?>"><?php echo $gname;?></option>
               <?php }
                ?>