开发者

How do I pass SQL database query from the Model to the Controller and then the View on Code Igniter 2.0.3?

I was trying to pass SQL values from M开发者_如何转开发odel to Controller but the value couldn't be passed.

This the code in my model file:

class Has_alert extends CI_Model {

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

function __get_query() {

$sql = 'alerts_get_alerts';
$query = $this->db->query($sql);
$row = $query->first_row();
$header_data['hasAlert'] = $row->active;    
}

}

And this is the code in my controller file:

class Chart extends CI_Controller {

// Default Constructor 
    public function __construct() {
    parent::__construct();
    $this->load->helper('html');
    $this->load->model('Has_alert', '', TRUE);
    $this->Has_alert->__get_query();



    //$sql = 'alerts_get_alerts';
    //$query = $this->db->query($sql);
    //$row = $query->first_row();
    //$header_data['hasAlert'] = $row->active;


}


public function index()
{

//Data Arrays
$this->load->helper('html');
$header_data['page_title'] = 'Title';
$header_data['tabid'] = "home";


//Load the headtop.php file and get values from data array
$this->load->view('includes/headertop.php', $header_data);
$this->load->view('homepage');
$this->load->view('includes/newfooter.php');

}

I got this error message on my view file:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: hasAlert

Filename: includes/headertop.php

Line Number: 184

Does anyone know what the problem is? Thank you.


Model

function __get_query() {
    $sql = 'alerts_get_alerts';
    $query = $this->db->query($sql);
    $row = $query->first_row();
    return $row->active;    
}

Controller

public function index(){
    $this->load->model("Has_alert");
    //Data Arrays
    $this->load->helper('html');
    $header_data['page_title'] = 'Title';
    $header_data['tabid'] = "home";
    $header_data['hasAlert'] = $this->Has_alert->__get_query();
    //Load the headtop.php file and get values from data array
    $this->load->view('includes/headertop.php', $header_data);
    $this->load->view('homepage');
    $this->load->view('includes/newfooter.php');
}

I'm assuming that things like "alerts_get_alerts" is pseudocode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜