从Codeigniter中的数据库到控制器获取动态内容

时间:2013-09-18 09:55:33

标签: php mysql codeigniter

我在数据库中有一个表,我保留了关键字/标题/描述。现在我想获取控制器函数::::

中的数据(内容)

如何将数据库中的数据转换为控制器::::

的函数
public function index()
{
   $data=array();

   $data['title']=" Title "; // need the title dynamic
   $data['keywords']="Keywords"; // need the keyword dynamic
   $data['keywords']="Description"; // need the description dynamic

}

3 个答案:

答案 0 :(得分:2)

试试这个

控制器中的

public function index()
{
    $data=array();
    $data['content']=$this->model_name->getContent();   

}

在模型中

function getContent()
{
    $data=array();
    return $data=$this->db->select('title,keywords,description')->get('table_name')->result_array();
}

这是从数据库中获取数据的一般方法。如果您遇到任何问题,请告诉我。

答案 1 :(得分:1)

试试这个。

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

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      $data['title'] = $row->title;
      $data['Keywords'] = $row->Keywords;
      $data['Description']= $row->Description;
   }
} 

您可以在视图文件中使用以下变量

$title, $Keywords, $Description.

答案 2 :(得分:0)

创建一个模型函数,该函数将查询数据库以返回控制器结果。您需要先了解CILearn Codeignter first!