1

我正在尝试将数组传递到视图页面并将项目放在列表框/下拉列表中。这段代码我哪里出错了?

模型

public function get_suppliers(){
        $type = "SUPPLIER";
        $this->db->where('usertype', $type);
        $query = $this->db->get('users');
        foreach ($query->result() as $row){
            $results = array(
                'userid' => $row->userid,
                'firstname' => $row->firstname,
                'lastname' => $row->lastname,
                'company' => $row->company
            );

        }
        return $results;
    }

控制器

$this->load->model('user_model');
$data['supplier']= $this->user_model->get_suppliers();
$this->load->view('include/header.php');
$this->load->view('addvehicle_view', $data);
$this->load->view('include/footer.php');

看法

<?php
  if(isset($supplier)){
      foreach ($supplier as $info){
      echo'<option value="' . $info->userid . '">' . $info->company . ' - ' . $info->lastname . ', ' . $info->firstname . '</option>';
    }
  }
  ?>
4

2 回答 2

3

get_suppliers()

$results = array(); // just in case there is no record

foreach (...) {
    $results[] = array( // you forgot the "[]"
        ...
    );
}

另一个问题:您的模型(一旦修复)返回一个数组数组,而您的视图需要一个对象数组。

直截了当,这是你的新性感模特方法:

public function get_suppliers() {
    return $this->db
        ->where('usertype', 'SUPPLIER')
        ->get('users')
        ->result();
}
于 2013-05-02T23:46:50.910 回答
0
public function get_suppliers(){
    $type = "SUPPLIER";
    $this->db->where('usertype', $type);
    $query = $this->db->get('users');
    $results = array();
    foreach ($query->result() as $row){
        $results[] = array(
                'userid' => $row->userid,
                'firstname' => $row->firstname,
                'lastname' => $row->lastname,
                'company' => $row->company
        );

    }
    return $results;
}
于 2013-05-03T04:43:21.950 回答