-1

有人可以告诉我如何通过模型(返回单行)从数据库中提取数据时将数据传递给视图:

我的示例代码

function read_article($article_name)
{
    $data['row'] = $this->user_model->extract_article($article_name);
    $this->load->view('user/view_article',$data);
}
  // controller code ^^^^^^^^^
function extract_article($article_name)
{
    $data = array();
    $this->db->select('col1,col2,col3,col4');
    $query = $this->db->get_where('table_name',array('col4'=>$article_name))->result();
    foreach($query as $s)
    {
        $data[] = $s;
    }
    $data;
}

// 型号代码 ^^^^^^^^^^

foreach($row as $r)
{
echo "<h1>".$r->col2."</h1>";
echo "<p>".$r->col3."</p>";
}

//查看代码^^^^^^^^^

遇到 PHP 错误

严重性:警告

消息:为 foreach() 提供的参数无效

文件名:用户/view_article.php

行号:52

4

1 回答 1

1

RTM

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row(); 

   echo $row->title;
   echo $row->name;
   echo $row->body;
} 
于 2013-09-06T18:21:03.420 回答