简体   繁体   English

CodeIgniter错误,result_array和foreach错误

[英]CodeIgniter error, result_array and foreach error

Below are my codes and errors that come up with my CodeIgniter. 以下是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? 我的错误是致命错误:在第15行的C:\\ wamp \\ www \\ CodeIgniter \\ application \\ models \\ news_model.php中的非对象上调用成员函数result_array()时,我找到了解决方法,但随后得到了错误,我的index.php文件第1行中的foreach语句出了点问题任何人都不知道错误可能在哪里?

news_model.php (model) 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 (controller) 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 (view) 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 ?>

Try returning $query->result() rather than $query->result_array() and check if it is working. 尝试返回$ query-> result()而不是$ query-> result_array()并检查其是否有效。
Also you will need to change in your foreach loop while displaying results. 同样,您需要在显示结果时更改foreach循环。

in your view you looping, $news variable as a result_array , 在您的视图中,循环$ news变量作为result_array

but in your model for : function get_news($slug = FALSE) , 但在您的模型中,该函数是: function get_news($ slug = FALSE)

if $slug !== FALSE your return row_array 如果$ slug!== 伪造您的返回row_array

I suggest you to return result_array() , so your view-code does not confuse. 我建议您返回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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM