Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

news_model.php (model)

<?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 (controller)

<?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 (view)

<?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 ?>
share|improve this question
 
Are there any records in the news table? –  Matthew Rapati Oct 29 at 20:26
 
I'd imagine if the query returns an empty set, result_array() should return an empty array. –  xbonez Oct 29 at 20:35
 
yes there are records. I am following codeIgniter why should it return an empty array when the array has fields? –  mazenchami Oct 30 at 11:58
add comment

2 Answers

Try returning $query->result() rather than $query->result_array() and check if it is working.
Also you will need to change in your foreach loop while displaying results.

share|improve this answer
 
errors have now been resolved. thank you! –  mazenchami Oct 30 at 12:45
add comment

my delete function doesn't work now any ideas?:

model: public function delete_news($id) //deleteing news { $this->db->delete('news', array('id' => $id)); //DELETE FROM news WHERE id=$id }

controller: public function delete($id) //delete controller { $this->news_model->delete_news($id); //calling delete_news($id) from our model redirect('/news/'); }

view:

Delete article

route: $route['news/delete/(:any)'] = 'news/delete/$1';

any ideas?

share|improve this answer
 
error free. positioning of route is important and making sure url's are in the correct positions also! –  mazenchami Oct 30 at 15:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.