2013-10-29 47 views
0

下面是我的CodeIgniter出現的代碼和錯誤。 我的錯誤是致命錯誤:調用第15行的C:\ wamp \ www \ CodeIgniter \ application \ models \ news_model.php中的非對象上的成員函數result_array()我發現了一個解決方法,但隨後我得到一個錯誤,在我的foreach語句在我的index.php文件行中有什麼錯誤1 任何人都有任何想法,錯誤可能?CodeIgniter錯誤,result_array和foreach錯誤

news_model.php(模型)

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class News_model extends CI_Model 
{ 

public function __construct() 
{ 
    $this->load->database(); 
} 

public function get_news($slug = FALSE) 
{ 
    if ($slug === FALSE) 
    { 
     $query = $this->db->get('news'); 
     return $query->result_array(); 
    } 

    $query = $this->db->get_where('news', array('slug' => $slug)); 
    return $query->row_array(); 
} 

public function set_news() 
{ 
    $this->load->helper('url'); 

    $slug = url_title($this->input->post('title'), 'dash', TRUE); 

    $data = array(
     'title' => $this->input->post('title'), 
     'slug' => $slug, 
     'text' => $this->input->post('text') 
    ); 

    return $this->db->insert('news', $data); 
} 


public function delete_news($id) 
{ 
    $this->db->delete('news', array('id' => $id)); 
} 
} 

news.php(控制器)

<?php 
class News extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('news_model'); 
} 

public function index() 
{ 
    $data['news'] = $this->news_model->get_news(); 
    $data['title'] = 'News archive'; 

    $this->load->view('templates/header', $data); 
    $this->load->view('news/index', $data); 
    $this->load->view('templates/footer'); 
} 

public function view($slug) 
{ 
    $data['news_item'] = $this->news_model->get_news($slug); 

    if (empty($data['news_item'])) 
    { 
     show_404(); 
    } 

    $data['title'] = $data['news_item']['title']; 

    $this->load->view('templates/header', $data); 
    $this->load->view('news/view', $data); 
    $this->load->view('templates/footer'); 
} 

public function create() 
{ 
    $this->load->helper('form'); 
    $this->load->library('form_validation'); 

    $data['title'] = 'Create a news item'; 

    $this->form_validation->set_rules('title', 'Title', 'required'); 
    $this->form_validation->set_rules('text', 'text', 'required'); 

    if ($this->form_validation->run() === FALSE) 
    { 
     $this->load->view('templates/header', $data); 
     $this->load->view('news/create'); 
     $this->load->view('templates/footer'); 

    } 
    else 
    { 
     $this->news_model->set_news(); 
     $this->load->view('news/success'); 
    } 
} 
} 

的index.php(視圖)

<?php foreach ($news as $news_item): ?> 

<h2><?php echo $news_item['title'] ?></h2> 
<div id="main"> 
    <?php echo $news_item['text'] ?> 
</div> 
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p> 

<a href="<?php echo base_url(); ?>news/delete/<?php echo $news_item['id'] ?>">Delete article</a> 

<?php endforeach ?> 
+0

新聞表中是否有記錄? –

+0

我想象如果查詢返回一個空集,'result_array()'應該返回一個空數組。 – xbonez

+0

是的,有記錄。我跟隨codeIgniter爲什麼它應該返回一個空數組,當數組有字段?現在已經解決了 – mazenchami

回答

1

嘗試返回$查詢 - >結果()而不是$ query-> result_array()並檢查它是否工作。
此外,您還需要在顯示結果時更改foreach循環。

+0

錯誤。謝謝! – mazenchami

0

在你看來,你的循環,$消息變量作爲result_array

但在你的模型:功能get_news($塞= FALSE)

如果$蛞蝓= = FALSE你的回報row_array

我建議你返回result_array(),讓您的視圖 - 代碼不會混淆。

public function get_news($slug = FALSE) 
{ 
    if ($slug === FALSE) 
    { 
     $query = $this->db->get('news'); 
     return $query->result_array(); //return result_array() 
    } 

    $query = $this->db->get_where('news', array('slug' => $slug)); 
    return $query->result_array(); //return result_array() 
}