for 发表于 2016-7-22 16:56:41

关于mvc的一个疑惑

有一个数据表news,里面包含了 di, title, content 等

在controller中,news.php 代码是

public function news($id)
    {
      $news = $this->news_m->get_newsdetail($id);
      if(!$news) {
            // 错误处理
      }
      else {
            $data['news'] = $news;
            $this->load->view('newsdetail',$data);
      }
    }


在model中,news_m.php

function get_newsdetail($id)
    {
      $this->db->from('news');
      $this->db->where( array('id'=>$id) );
      $news = $this->db->get()->result_array();
      if( is_array($news) && count($news) > 0 ) {
          return $news;
      }
      return false;
    }


在view中,newsdetail.php


<?php foreach( $news as $newsone ) : ?>
                  <tr>
                  <td><?php echo $newsone['title'] ?></td>
                  <td><?php echo $newsone['content'] ?></td>
                  </tr>
<?php endforeach; ?>


能正常显示内容,但这里有个问题不是太明白,我这里只是想获取某一条新闻的内容,不是全部新闻的内容,那么在 newsdetail.php 中这样写对吗?因为多条新闻的内容列表也是这样写的,如果我写的不对,正确的做法是?

Closer 发表于 2016-7-22 17:30:23

result_array() 改用 row_array()

Aloghli 发表于 2016-7-23 10:19:12

如果只要获取一条数据的话就得用row()或者row_array()
页: [1]
查看完整版本: 关于mvc的一个疑惑