如何在codeigniter中通过id获取文章

时间:2014-04-11 19:09:05

标签: php codeigniter

例子我有3篇文章每篇文章都有按钮“阅读更多”。我得到1篇文章有这样的数组。

    Dump => object(stdClass)#21 (6) {
  ["id"] => string(2) "76"
  ["id_admin"] => string(0) ""
  ["judul_berita"] => string(5) "kasus"
  ["content"] => string(449) "                                
                              This is my first use of nestedsortable.js in a custom cms creation with codeigniter, So I use it with Codeigniter php framework. I have an ordered and nested list(for order pages) and I need to convert this list to array with 'toArray' but it's not working the error message: uncaught typeError: Cannot call method 'match' of undefined this is my js code:
                            "
  ["tanggal"] => string(10) "2014-03-25"
  ["slug"] => string(5) "kasus"
}

和另外两篇文章

    Dump => array(0) {
}

错误代码在哪里?

我的控制器

    public function index($id = null, $slug = FALSE){
 // Fetch the article
        $this->db->where('pubdate <=', date('Y-m-d'));
        $this->data['article'] = $this->mberita->get_by_id($id,$slug);
        dump($this->data['article']);
       }

我的模特

function get_by_id($id, $slug)
    {
        return $this->db->get_where($this->tbl_berita, array('id' => $id,  'slug' => $slug),1)->row();
    }

请帮我做什么。谢谢。

请检查此代码是否有任何错误?

// Redirect if slug was incorrect
    $requested_slug = $this->uri->segment(3);
    $set_slug = $this->data['article']->slug;
    if ($requested_slug != $set_slug) {
        redirect('article/' . $this->data['article']->id . '/' . url_title($this->data['article']->slug), 'location', '301');
    }

1 个答案:

答案 0 :(得分:0)

我认为是因为row()检索单行。更好地使用fetch_object或任何其他方法。

您可以在codeigniter文档中了解更多相关信息 - here