1

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.

5
  • Check the value is getting on the controller by print_r($data['high']); exit; Commented Mar 24, 2015 at 9:05
  • can you check if $this->Alert_result_model->countHighRisk(); returns correct variable Commented Mar 24, 2015 at 9:06
  • echo $data['high']; after the database call...is that printing any value;? Commented Mar 24, 2015 at 9:06
  • IS YOUR QUERY RETURNS ANYTHING? check it first IS YOUR QUERY RIGHT ? try echo $this->db->last_query();exit; below $query Commented Mar 24, 2015 at 9:08
  • Thank you guys, for the fast response. Apparently there is nothing wrong with the code. I just restarted my localhost and now it works! Commented Mar 24, 2015 at 9:11

3 Answers 3

1

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();)

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

4 Comments

Thanks, but the problem is already fixed. There was no error in my code. I just restarted localhost. But are you saying that I have to call other views in a view? In the documentation of codeigniter they say that you have to call multiple views in the controller. ellislab.com/codeigniter/user-guide/general/views.html
I am not really fan of code igniter documentation, but yeah, its a bit messy when you are calling 3 static lines over and over again in controllers, so why not just call them in views instead. Remember documentation is not word of God :)
The documentation you shared is just an example, which is not architecturally correct in 90% of the cases.
Thanks, I agree it looks a bit messy. So I will do it like you say :)
1

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.

1 Comment

i have read the article about how to pass data From Controller To View In CodeIgniter cloudways.com/blog/how-to-pass-data-in-codeigniter
0

please change

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

into

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

now it should work.

1 Comment

Thanks, but this was not necessary I just restarted my localhost and now it works just fine.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.