如何在codeigniter中显示sql表字段

时间:2016-09-15 17:50:47

标签: php codeigniter

我正在尝试在索引页面上显示我的“项目”表的字段内容,但似乎无法使其正常工作。我在这里做错了什么?

型号:

public function prodView() {
            $sql = 'SELECT * FROM projects';
            $query = $this->db->query($sql);
            return $query->result();
}

控制器:

public function index() {
         $this->load->model('main_model');
         $data ['query'] = $this->main_model->prodView(); 
         $this->load_view('/index', $data);
}

查看:

<?php foreach($query as $row): ?>
  <tr> 
    <td><?php echo $row->project_name; ?></td>
  </tr>
<?php endforeach; ?>

1 个答案:

答案 0 :(得分:1)

<强>模型

public function prodView() {
  $result = array();
  $this->db->select("*")->from("projects");
  $query = $this->db->get();
  if($query->num_rows() > 0){
     $result = $query->result_array();
  }
  return $result;
}

<强>控制器

public function index() {
   $this->load->model('main_model');
   $data ['query'] = $this->main_model->prodView(); 
   $this->load->view('missio/index', $data);
}

查看

<?php foreach($query as $row): ?>
  <tr> 
    <td><?php echo $row['project_name']; ?></td>
  </tr>
<?php endforeach; ?>