Codeigniter global variable in view

BGKND

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?

ourmandave

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.';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

View to global class variable?

From Dev

CodeIgniter counter using "global" variable

From Dev

how to define Global variable in codeigniter?

From Dev

Call variable from view in Codeigniter

From Dev

Call variable from view in Codeigniter

From Dev

How to use global variable in PHP Codeigniter

From Dev

How to define a global variable(value) in codeIgniter

From Dev

Print Global variable in view using angular

From Dev

Codeigniter: Passing Data to View Undefined variable

From Dev

Unable to pass variable from codeigniter controller to the view?

From Dev

Passing variable from controller to view not working codeigniter

From Dev

How to send single variable to view in codeigniter

From Dev

Laravel 5 - global Blade view variable available in all templates

From Dev

Avoid view repetition by reusing title and other key tidbits in a global variable

From Dev

How can a Router talk to a View other than using a global variable?

From Dev

Calling Global Variable from Controller to HTML View AngularJS

From Dev

Expected declaration while accessing global variable from second view controller

From Dev

Update angular2 View/global variable with data inside a promise

From Dev

Global variable which may or may not change on each view Laravel 5.2

From Dev

Global variable returning nil value when used in diffrent view controller

From Dev

How to pass a value of a variable from view to controller in codeigniter

From Dev

Codeigniter Do I need to check if a variable is set in view?

From Dev

How to pass Array Variable (Blade Template) to CodeIgniter View?

From Dev

How can I pass a variable to a view, in Codeigniter 3, if it is not in the URL

From Dev

Pass php variable from view to controller to model using codeigniter

From Dev

codeigniter global array for a model

From Dev

local variable and global variable

From Dev

Global state vs global variable

From Dev

Javascript global module or global variable

Related Related

  1. 1

    View to global class variable?

  2. 2

    CodeIgniter counter using "global" variable

  3. 3

    how to define Global variable in codeigniter?

  4. 4

    Call variable from view in Codeigniter

  5. 5

    Call variable from view in Codeigniter

  6. 6

    How to use global variable in PHP Codeigniter

  7. 7

    How to define a global variable(value) in codeIgniter

  8. 8

    Print Global variable in view using angular

  9. 9

    Codeigniter: Passing Data to View Undefined variable

  10. 10

    Unable to pass variable from codeigniter controller to the view?

  11. 11

    Passing variable from controller to view not working codeigniter

  12. 12

    How to send single variable to view in codeigniter

  13. 13

    Laravel 5 - global Blade view variable available in all templates

  14. 14

    Avoid view repetition by reusing title and other key tidbits in a global variable

  15. 15

    How can a Router talk to a View other than using a global variable?

  16. 16

    Calling Global Variable from Controller to HTML View AngularJS

  17. 17

    Expected declaration while accessing global variable from second view controller

  18. 18

    Update angular2 View/global variable with data inside a promise

  19. 19

    Global variable which may or may not change on each view Laravel 5.2

  20. 20

    Global variable returning nil value when used in diffrent view controller

  21. 21

    How to pass a value of a variable from view to controller in codeigniter

  22. 22

    Codeigniter Do I need to check if a variable is set in view?

  23. 23

    How to pass Array Variable (Blade Template) to CodeIgniter View?

  24. 24

    How can I pass a variable to a view, in Codeigniter 3, if it is not in the URL

  25. 25

    Pass php variable from view to controller to model using codeigniter

  26. 26

    codeigniter global array for a model

  27. 27

    local variable and global variable

  28. 28

    Global state vs global variable

  29. 29

    Javascript global module or global variable

HotTag

Archive