0

I was trying to pass SQL values from Model 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.

3
  • You're not calling model, in your controller... and furthermore, the model does not return any value. Commented Sep 14, 2011 at 14:28
  • Hi Alfonso, I thought this code is the code that is being used to call model in the controller:$this->load->model('Has_alert', '', TRUE); $this->Has_alert->__get_query(); Commented Sep 14, 2011 at 14:41
  • You're right, not what I saw in the first reading. My solution should work anyway. Commented Sep 14, 2011 at 14:46

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The alerts_get_alerts is a store procedure. I tried your code but I got a different error message: PHP Fatal error: Call to undefined method Has_alert::__get_query() in C:\www\newchart\application\controllers\chart.php on line 10
Alfonso, you are awesome! Thank you once again. The error went away! It works! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.