Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:40:34+00:00 2026-06-03T07:40:34+00:00

I am trying to initialize data in index function of controller, so that initialized

  • 0

I am trying to initialize data in index function of controller, so that initialized data can be used in subsequent functions of controller. But the problem is data is not being displayed when I am trying to access it from other function. All of this is just to follow a sort of object oriented pattern.

Here is my code.

class Dashboard extends CI_Controller
{
    private  $account_data;  /*Declaration*/
    private  $profile_data;

    function __construct() {
       // code...
    }

    function index()   /*Here I am initializing data*/
    {
        $this->load->model('db_model');
        $this->account_data = $this->db_model->get_row();
        $this->profile_data = $this->db_model->get_row();
        $this->load->view('user/dashboard');
    }

    function function account_details()
    {
        print_r($this->account_data);  // This displays nothing
    }

    /*other function...*/

}

Idea is to get data once and use it for other functions and if data is updated again calls a function to initialize it.

But it is not working out. Please help me. Also suggest if I am following right approach.
Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:40:35+00:00Added an answer on June 3, 2026 at 7:40 am

    index method is not initializer, its default page/sub_method,
    if you call the “*account_details*” in url as index.php/dashboard/account_details the index wont be called.

    try put the code on constructor,

    class Dashboard extends CI_Controller
    {
        private  $account_data;  /*Declaration*/
        private  $profile_data;
    
        function __construct() { /*Here I am initializing data*/
          parent::CI_Controller(); // Thank you Sven
            $this->load->model('db_model');
            $this->account_data = $this->db_model->get_row();
            $this->profile_data = $this->db_model->get_row();
        }
    
        function index()   
        {
    
            $this->load->view('user/dashboard');
        }
    
        function function account_details()
        {
            print_r($this->account_data);  // This displays nothing
        }
    
        /*other function...*/
    
    }
    

    Note : don’t the models or other computations on __construct() if you don’t need on all methods of this controller.

    create a private method like “model_initializer()” put this codes on this scope, and the call it in your other methos as $this->model_initialize(); if you need.

    Thanks yo Sesama Sesame for note,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to initialize directX, but for some reason it can't create d3d
I'm trying to initialize my data in my Azure Data Tables but I only
I'm trying to initialize a std::vector from a std::list efficiently, but I'm not having
I am trying to initialize NSDictionary but it gives me following error: Program received
Edited again because it originally wasn't clear that I'm trying to initialize the arrays
Can I declare an int array, then initialize it with chars? I'm trying to
I'm trying to initialize a dict variable but I don't understand why one way
I am trying to get the index replication to work and I can't seem
I'm having a problem adding an item to my tableView. I used to initialize
I am trying to initialize the MySQL data-base relation by words written in Polish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.