【问题标题】:Get dynamic content from database to controller in Codeigniter在 Codeigniter 中从数据库获取动态内容到控制器
【发布时间】:2013-09-18 09:55:33
【问题描述】:

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

如何从数据库中获取数据到控制器的函数 ::::

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

}

【问题讨论】:

  • 在模型中创建查询并在控制器中调用该函数
  • 非常感谢。我明白了...

标签: php mysql codeigniter


【解决方案1】:

试试这个

在控制器中

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();
}

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

【讨论】:

  • 非常感谢....现在如何在我的视图文件中显示它们?喜欢::: ????请
  • 非常感谢。我明白了
  • 是使用 显示。如果对您有帮助,请接受答案。谢谢
【解决方案2】:

试试这个。

$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.

【讨论】:

  • 感谢您的回答。我如何在 head 标签的视图页面中显示它们?请
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多