1

我试图从数据库中获取网站任何页面中的滚动新闻,而不必在网站的每个控制器中传递变量。所以我决定使用钩子而不是在每个控制器中传递滚动变量。

我确实创建了一个这样的类

class Scroll
{

 function getScroller()
 {

  $data = array();

  $CI =& get_instance();

  $CI->db->where('a_status','active');
  $CI->db->limit(4);
  $CI->db->order_by('id','desc');

  $Q = $CI->db->get('news');
  if($Q->num_rows() > 0){
    foreach($Q->result_array() as $row){
        $data[] = $row;
    }
  }
  $Q->free_result();
  return $data;
 }



}

我现在得到的是

Severity: Notice

Message: Trying to get property of non-object
Call to a member function get() on a non-object in E:\xampp\htdocs\

谁能帮我怎么做?谢谢,我想在任何控制器的视图中自动获取滚动新闻,而无需传入每个控制器。谢谢

4

3 回答 3

0

如果您在视图级别上定义它,则无需这样做。

您可以直接在视图中定义数据库请求。

其他方法是使用具有单独视图的分离控制器并通过 iframe 将其加载到页面中。它通常用于稍后可以加载到另一个页面的“网络小部件”。

于 2013-04-09T06:09:37.757 回答
0

扩展 CI Controller 的核心类应该会给你带来更少的麻烦。 http://ellislab.com/codeigniter/user-guide/general/core_classes.html

应用程序/核心/MY_Controller.php

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();

        //By do this, all controllers who use this class as parent controller
        //will have $news in their views
        $this->load->vars(array( 
             'news' =>  array()
        ));
    }   
}

应用程序/控制器/welcome.php

class Welcome extends MY_Controller {

    public function index()
    {       
        $this->load->view('welcome_message');
    }
}

应用程序/视图/welcome_message.php

var_dump($news);
于 2013-04-09T21:10:04.020 回答
0

使用分离的库或助手并在控制器的构造上调用该方法,例如:

class My_Controller extends CI_Controller(){

    function __construct(){
      parent::__construct();
    //load your library
    //call your library method
    }
    }
于 2013-04-09T21:36:58.343 回答