Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter error, result_array and foreach error
    primarykey
    data
    text
    <p>Below are my codes and errors that come up with my CodeIgniter. My error is Fatal error: Call to a member function result_array() on a non-object in C:\wamp\www\CodeIgniter\application\models\news_model.php on line 15 I found a way around that but then I get an error that something is wrong in my foreach statement in my index.php file line 1 Anyone have any idea where the error maybe?</p> <p>news_model.php (model)</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class News_model extends CI_Model { public function __construct() { $this-&gt;load-&gt;database(); } public function get_news($slug = FALSE) { if ($slug === FALSE) { $query = $this-&gt;db-&gt;get('news'); return $query-&gt;result_array(); } $query = $this-&gt;db-&gt;get_where('news', array('slug' =&gt; $slug)); return $query-&gt;row_array(); } public function set_news() { $this-&gt;load-&gt;helper('url'); $slug = url_title($this-&gt;input-&gt;post('title'), 'dash', TRUE); $data = array( 'title' =&gt; $this-&gt;input-&gt;post('title'), 'slug' =&gt; $slug, 'text' =&gt; $this-&gt;input-&gt;post('text') ); return $this-&gt;db-&gt;insert('news', $data); } public function delete_news($id) { $this-&gt;db-&gt;delete('news', array('id' =&gt; $id)); } } </code></pre> <p>news.php (controller)</p> <pre><code>&lt;?php class News extends CI_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;model('news_model'); } public function index() { $data['news'] = $this-&gt;news_model-&gt;get_news(); $data['title'] = 'News archive'; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/index', $data); $this-&gt;load-&gt;view('templates/footer'); } public function view($slug) { $data['news_item'] = $this-&gt;news_model-&gt;get_news($slug); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/view', $data); $this-&gt;load-&gt;view('templates/footer'); } public function create() { $this-&gt;load-&gt;helper('form'); $this-&gt;load-&gt;library('form_validation'); $data['title'] = 'Create a news item'; $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required'); $this-&gt;form_validation-&gt;set_rules('text', 'text', 'required'); if ($this-&gt;form_validation-&gt;run() === FALSE) { $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('news/create'); $this-&gt;load-&gt;view('templates/footer'); } else { $this-&gt;news_model-&gt;set_news(); $this-&gt;load-&gt;view('news/success'); } } } </code></pre> <p>index.php (view)</p> <pre><code>&lt;?php foreach ($news as $news_item): ?&gt; &lt;h2&gt;&lt;?php echo $news_item['title'] ?&gt;&lt;/h2&gt; &lt;div id="main"&gt; &lt;?php echo $news_item['text'] ?&gt; &lt;/div&gt; &lt;p&gt;&lt;a href="news/&lt;?php echo $news_item['slug'] ?&gt;"&gt;View article&lt;/a&gt;&lt;/p&gt; &lt;a href="&lt;?php echo base_url(); ?&gt;news/delete/&lt;?php echo $news_item['id'] ?&gt;"&gt;Delete article&lt;/a&gt; &lt;?php endforeach ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload