0

I don't know if the title above is an correct title about my question.

Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?

for example in controller file Students.php contains many function that handle the views, take a look below :

public function __construct() {
  parent::__construct();
  $data['count_student'] = $this->m_data->allStudent(); // count all students
}

public function index() {
  $data['content'] = 'view-students';
  $this->load->view('frontend/header' , $data);
}

public function showDetails() {
      $data['content'] = 'view-detail-students';
      $this->load->view('frontend/header' , $data);
}

I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.

Is there possible ways to do that?

3 Answers 3

1

In the application/core/ create MY_Controller.php

<?php 

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_data');
        $this->data['count_student'] = $this->m_data->allStudent();
    }

}

Controller use $this->data

class Students extends MY_Controller {

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

    public function index() {
      $this->data['content'] = 'view-students';
      $this->load->view('frontend/header', $this->data);
    }

    public function showDetails() {
        $this->data['content'] = 'view-detail-students';
        $this->load->view('frontend/header', $this->data);
    }
}

Now that you have extended the controller you shoudld be able to just go like

<?php echo $count_student;?>

On the view

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

2 Comments

this method seems to be complicated for me, but it's woking. Thankyou, you just show to me another ways to deal with codeigniter.
@KimJongUn quite simple
0

I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.

 public function __construct() {
   parent::__construct();
   // $data['count_student'] = $this->m_data->allStudent(); // count all students
   // 
   $this->count_all_the_students = $this->m_data->allStudents();

 }

 public function index() {
   $data['content'] = 'view-students';
   $this->load->view('frontend/header' , $data);
 }

And then in the view you can use $this->count_all_the_students without putting it in the $data array.

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

 <?php 
   echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';

1 Comment

it's working bro. simple solution and I choose this for the correct answers.
0

If you are using the same variable in every controller function then you have to declare it as follows

class Example extends CI_Controller{
protected $data = array();

public function __construct() {
  parent::__construct();
  $this->data['count_student'] = $this->m_data->allStudent();
}

public function index() {
  $this->data['content'] = 'view-students'; //variables are same
  $this->load->view('frontend/header' , $this->data);
}

public function showDetails() {
      $this->data['content'] = 'view-detail-students'; //variables are same
      $this->load->view('frontend/header' , $this->data);
}

}

This applies only if you have common variables for all views in this controller. If you are using different variables then use the following code.

class Example extends CI_Controller{
    protected $count_student= "";

public function __construct() {
  parent::__construct();
  $this->count_student = $this->m_data->allStudent();
}

public function index() {
  $data['content'] = 'view-students'; //Variable is different
  $data['count_student'] = $this->count_students;
  $this->load->view('frontend/header' , $data);
}

public function showDetails() {
      $data['details'] = 'view-detail-students'; // Variable is different
      $data['count_student'] = $this->count_students;
      $this->load->view('frontend/header' , $data);
}

}

2 Comments

first letter only of the controller should be upper case also for the filename and classname codeigniter.com/user_guide/general/styleguide.html#file-naming
You are right buddy. That wasn't intentional. I just copied and pasted his code. Anyhow, I'll correct it.

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.