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 8466277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:17:42+00:00 2026-06-10T15:17:42+00:00

I want to pass a variable from a controller to two view files. public

  • 0

I want to pass a variable from a controller to two view files.

    public function post($id) {
    $data['query'] = $this->blog_model->get_post($id);
    $data['comments'] = $this->blog_model->get_post_comment($id);
    $data['post_id'] = $id;
    $data['total_comments'] = $this->blog_model->total_comments($id);

I want to pass the [total_comments] variable to the index.php and post.php views. How do I do that? Can I pass data to a view without loading them like this:

     $this->load->view('post',$data);
  • 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-10T15:17:44+00:00Added an answer on June 10, 2026 at 3:17 pm

    Something along this lines?

    $data['post'] = $this->load->view('post',$data, TRUE);
    

    The 'TRUE' argument tells CI to call your view and place it in a $data['post'] variable. Later on you can use that variable in another view and just print it out.

    edit:

    I’m not sure how you have organized your controllers and views but lets say something like this. This is just an example:

    controller

     public function comments() {
            $data['comments'] = $this->comments_model->get_all_comments();
            $data['someVariable'] = 123;
            $this->load->view('header', $data);          //load header view
            $data['sidebar'] = $this->load->view('sidebar', $data, TRUE);   //put sidebar view in a variable, but don't show it immediately 
            $this->load->view('comments', $data);        //load comments view
    
            $this->load->view('footer'. $data);          //load footer view
        }
    

    Whenever you pass $data to a controller you are passing the whole $data array to that view so you can access all of its elements in a view.

    For example in your comments.php view you will have $comments, $someVariable and $sidebar variables so you can do whatever you want with them.

    In comments.php you’d probably have something like this:

    comments.php

    <div id="comments"> 
    <?php 
       foreach($comments as $c){       //print out all found comments
    ?>
       <div class="comment">
          <?= $c['commentauthor'] ?> <br />
          <?= $c['commenttext'] ?>
       </div>
    <?php } ?>
    </div>
    
    <div id="sidebar">
      <?= $sidebar ?>       //print out sidebar
    </div>
    
    <p> This is some variable: <?= $someVariable ?> </p>
    

    Those same variables are available in the footer view, because you’ve passed $data to that view

    $this->load->view('footer'. $data);
    

    I hope that this makes things a bit more clear to you.

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

Sidebar

Related Questions

I am able to pass a variable forward from view controller to view controller
I want to pass a variable as text or somehow to make above function
I want to pass external XML a variable. How do I do this? WHAT
I need to pass a url string from the view to the controller, a
I want to pass javascript object/ variable from page to another. so that i
I want to ask how to pass a variable from a page to another
I would like to pass the variable from a function to the main scope
i want to pass a variable from a viewcontroller to another the way i
I want to pass variable arguments obtained in one function to other function but
I want to pass the variable from php page to another without using sessions.

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.