Sven Habex
Sven Habex

Reputation: 11

CodeIgniter pass variable from controller to view

Sorry if this question is already been answered, but i couldn't find an answer. I want to pass a variable from my model to the controller and finally display it in the view.

I execute a query on the database and after that I count my results (via num_rows). Then I return the result.

public function countHighRisk(){
    $this->db->select("ares_status");
    $this->db->from("tbl_alert_result");
    $this->db->join('tbl_status_standards', 'ares_status = sts_status_id');
    $this->db->where('sts_status_risk', 3);
    $query = $this->db->get();

    return $query->num_rows();
}

After that i place the result in an array and pass the array to my view.

public function index()
{
    $this->load->model('Alert_result_model');
    $data['high'] = $this->Alert_result_model->countHighRisk();

    $this->load->view('templates/head');
    $this->load->view('templates/menu');
    $this->load->view('pages/Dashboard', $data);
    $this->load->view('templates/footer');
}

I get an error when I try to display the variable in the view.

<div class="col-xs-8 text-right">
     <span> High Risk </span>
     <h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>

error: https://i.sstatic.net/8cvLO.jpg

Thanks in advance.

Upvotes: 1

Views: 7285

Answers (3)

varatha raj
varatha raj

Reputation: 1

please change

$this->load->view('pages/Dashboard', $data);

into

$this->load->view('pages/Dashboard', $data, true);

now it should work.

Upvotes: 0

user4419336
user4419336

Reputation:

You can load a view into controller but do this way below

class Dashboard extends CI_Controller {
 public function index() {

  $this->load->model('Alert_result_model');
  $data['high'] = $this->Alert_result_model->countHighRisk();

  // Choose only one header examples:
  $data['header'] = $this->load->view('header', Null, TRUE); // If No Data
  $data['header'] = $this->load->view('header', $data, TRUE); // If Data

  // Choose only one Footer examples:
  $data['footer'] = $this->load->view('footer', Null, TRUE); // If No Data
  $data['footer'] = $this->load->view('footer', $data, TRUE); // If Data

  $this->load->view('dashboard', $data);
 }
}

Dashboard View

<?php echo $header;?>

<?php foreach ($high as $low) { ?>

   <?php echo $low['something'];?>

<?php }?>

<?php echo $footer;?>

If you have a header separate header controller and footer controller you will need HMVC if you are confused on user guide top right corner of default user guide in CI3 is a switch to more cleaner version.

Upvotes: 1

DJ&#39;
DJ&#39;

Reputation: 1770

Your whole idea of calling the views is wrong here. Please use it as follows:

Controller:

public function index()
{
    $this->load->model('Alert_result_model');
    $data['high'] = $this->Alert_result_model->countHighRisk();

    $this->load->view('pages/Dashboard', $data);
}

View:

<? $this->load->view('templates/head'); ?>
<? $this->load->view('templates/menu'); ?>
<div class="col-xs-8 text-right">
     <span> High Risk </span>
     <h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
<? $this->load->view('templates/footer'); ?>

Also check what $this->Alert_result_model->countHighRisk(); is actually returning by doing print_r($this->Alert_result_model->countHighRisk();)

Upvotes: 1

Related Questions