php - CodeIgniter - Sending array from model to controller to page -
i trying pass array view page , put items in listbox/dropdown. going wrong in code?
model
public function get_suppliers(){ $type = "supplier"; $this->db->where('usertype', $type); $query = $this->db->get('users'); foreach ($query->result() $row){ $results = array( 'userid' => $row->userid, 'firstname' => $row->firstname, 'lastname' => $row->lastname, 'company' => $row->company ); } return $results; }
controller
$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');
view
<?php if(isset($supplier)){ foreach ($supplier $info){ echo'<option value="' . $info->userid . '">' . $info->company . ' - ' . $info->lastname . ', ' . $info->firstname . '</option>'; } } ?>
in get_suppliers()
:
$results = array(); // in case there no record foreach (...) { $results[] = array( // forgot "[]" ... ); }
another issue: model (once fixed) returns array of arrays, whereas view expects array of objects.
straight point, here's new sexy model method:
public function get_suppliers() { return $this->db ->where('usertype', 'supplier') ->get('users') ->result(); }
Comments
Post a Comment