1

我试图从控制器发送到视图中的所有联系人的数组,这些联系人在我的数据库表“联系人”中,然后将它们显示在下拉菜单上。我遵循了关于此主题的 CodeIgniter 文档http://codeigniter。 com/user_guide/general/views.html但这并不是我真正想做的。这是我试图做的:

function getAll_contact(){
     $exist= $this->contacts_model->get_all('contacts');
     if($exist)
        {
            $all_contact = $this->contacts_model->read('contacts');
        //echo json_encode($all_contact); prints all the contacts in the table
                    $this->load->view('myView', $contact);
        }
    }

在我看来 :

      <select class="span4">
          <?php if(isset($all_contact) && ! empty($all_contact)){ 
            foreach($all_contact as $contact){
            echo "<option value='".$contact->id_contact."'>".$contact->company."</option>";


    }
}
      </select>

这不会在下拉菜单上显示任何内容。有人可以帮我吗?

4

2 回答 2

2

将您的结果放入数据数组中..

$data['all_contact']=$this->contacts_model->read('contacts');

并将数组发送到查看

$this->load->view('myView', $data);

并且您可以使用 $all_contact 在您的视图中获取该变量 ..就像您目前拥有的一样..

于 2012-09-24T14:07:17.860 回答
0

首先,您将变量命名为 $contact 而不是 $all_contact。

解决方案:

function getAll_contact(){
  $exist= $this->contacts_model->get_all('contacts');
  if($exist)
  {
    $data['all_contact'] = $this->contacts_model->read('contacts');
    //echo json_encode($data); prints all the contacts in the table
    $this->load->view('myView', $data);
  }
}

然后您可以像当前在您的视图中一样访问它。

于 2012-09-24T14:04:52.737 回答